Note that there are some explanatory texts on larger screens.

plurals
  1. POWeb app, what is the proper way to catch exceptions, using the example here?
    text
    copied!<p>Lately, I've been reading on how to properly handle exceptions to improve my software (avoid catch generic exceptions and all), but I'm still a bit confused. Here is what I currently do (which violates some of the good practices, or maybe all of it)</p> <p>ASP.NET web app calls business layer, which in return calls data access layer. Log framework used is log4net, which will email me whenever an exception is logged. </p> <p>Data access layer catch generic exception (yeah, not good, as I don't know if a network could be the problem, or if an input to a store procedure is a wrong type, etc) log it as fatal and then "throw;" (to keep the stack trace). </p> <pre><code>catch(Exception ex) { log.Fatal("Exception occurred while inserting task into database",ex); throw; } </code></pre> <p>*<strong>Note</strong>, my data access layer assumes all input validations are done at the upper layers, but sometimes, I missed checking one or two parameters. </p> <p>Business layer catch "generic" exception as well (including the exception from data access), then wraps around a new exception as in </p> <pre><code> catch(Exception ex) { log.Fatal("Exception occurred while creating the task ", ex); throw new exception("There appears to be an error while doing [name of business operations]", ex) } </code></pre> <p>Web application layer catch any exception from the business layer and display the error message. </p> <pre><code>catch(Exception ex) { //show Ex.Message to the user on the current UI. } </code></pre> <p>In addition, to a global error message that redirects users to a generic "contact admin" error page. </p> <p>From all I have read, seems like I'm doing it ALL wrong. In that case, using the example above, how should I have properly handled exceptions at each level? </p> <ul> <li>Data Access </li> <li>Business Layer </li> <li>Web Application </li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload