error handling advicehttp://forums.asp.net/t/1791157.aspx/1?error+handling+adviceTue, 17 Apr 2012 02:13:43 -040017911574924731http://forums.asp.net/p/1791157/4924731.aspx/1?error+handling+adviceerror handling advice <p>Hello,</p> <p>My application is near completion and currently being beta tested at my work place by others. While on my development system everything seems stable, in the wild it's&nbsp;shown to be less stable than I thought.</p> <p>Hence I'm wanting to write some robust error handling into the application. Right now the largest instances of errors happen when doing an 'insert' or 'update'.</p> <p>So far I've managed to loose server connections on a few occasions through unmanaged insert / update errors, resulting in server restarst to fix.</p> <p>I've added code below and I an wondering whether I'm on the right track, or if there is a better way to handle insert, update errors.</p> <p>Any advice appreciated..</p> <pre class="prettyprint">@{ var userErrMsg = &quot;&quot;; var errMsg = &quot;&quot;; var actiontype = Request[&quot;action-type&quot;]; var projectno = Request[&quot;projectno&quot;]; var description = Request[&quot;Description&quot;]; var db = Database.Open(App.conDatabase); try { var row = processSQL.getUsername(App.conDatabase, WebSecurity.CurrentUserName); if(actiontype == &quot;new&quot;) { var author = row.Username; var createddate = DateTime.Now; var sql = &quot;INSERT INTO tblProjectBenefits (Description, ProjectRef, Author , CreatedDate) VALUES (@0, @1, @2, @3)&quot;; db.Execute(sql, description, projectno ,author, createddate); } //edit user post else if(actiontype == &quot;edit&quot;) { var id = Request[&quot;ID&quot;]; var updateby = row.Username; var updatedate = DateTime.Now; var sql = &quot;UPDATE tblProjectBenefits SET Description = @0, ProjectRef = @1, UpdateBy = @2, UpdateDate = @3 WHERE ID = @4&quot;; db.Execute(sql, description, projectno, updateby, updatedate, id); } else if(actiontype == &quot;delete&quot;) { var id = Request[&quot;ID&quot;]; var sql = &quot;DELETE FROM tblProjectBenefits WHERE ID=@0&quot;; db.Execute(sql, id); } } catch (Exception ex) { // You can use the exception object for debugging, logging, etc. errMsg = ex.Message; // Create a friendly error message for users. userErrMsg = &quot;An exception has occured, please contact &quot; &#43; &quot;your system administrator.&quot;; } finally { //Any cleanup code db.Close(); } }</pre> <p><br> <br> </p> <p></p> 2012-04-10T11:11:25-04:004924934http://forums.asp.net/p/1791157/4924934.aspx/1?Re+error+handling+adviceRe: error handling advice <p>If I do not know what is the error, I would not catch.</p> <p></p> 2012-04-10T12:37:40-04:004926685http://forums.asp.net/p/1791157/4926685.aspx/1?Re+error+handling+adviceRe: error handling advice <p>Okay, be specific about what errors I want to catch..</p> <p>Should I also be closing the db connection everytime I make a call?</p> <p>does the<em> finally { }</em> run on every connection even if there is no failure?</p> <p></p> 2012-04-11T10:27:23-04:004935646http://forums.asp.net/p/1791157/4935646.aspx/1?Re+error+handling+adviceRe: error handling advice <p>Hi</p> <p>For the exception you should know what exception may cause, then right like this.</p> <pre class="prettyprint">catch (InvalidCastException e) //http://msdn.microsoft.com/en-us/library/0yd65esw(v=vs.80).aspx { }</pre> <p><br> And for finally{ } it will run in any situation, so put db.close in finally is right.</p> 2012-04-17T02:13:43-04:00