views:

47

answers:

3

1) To my understanding, in a three-tier Asp.Net application we should implement exception handling in the following way:

a - we should put try-catch block around block of code ( located in any of the three layers ) from which we expect the page to gracefully recover from ( when this code generates an exception )?

b - we shouldn’t put try-catch blocks around code ( located in either of the three layers ) from which we don’t expect the page to gracefully recover from. Instead, Asp.Net application should always manage these unhandled exceptions through global exception handler ( Application_Error/Page_Error )?

2) Is the main benefit of managing unhandled exceptions through Application_Error/Page_Error the fact that this way we centralize error handling in one location?

After all, we could achieve the same results even if these unhandled exceptions (thrown in any of the three layers ) were instead handled ( logged, user redirected to custom error page ... ) at the spot where they were thrown?!

thank you

+2  A: 

Not necessarily.

1a - it's quite acceptable for the data layer to have no exception handling. In which case any exception would be handled further up the stack.

1b - the data layer and business layer may not be called via a website, so a particular web page isn't necessarily relevant. For example, a webservice or WPF application may also use these layers.

2 - yes the main benefit is a single location for website error handling.

Joe R
“ 1b - the data layer and business layer may not be called via a website, so a particular web page isn't necessarily relevant.”I’m not sure about the point you’re trying to make?!
A WPF application may use the business or data layer in the future, so there shouldn't be any website specific logic in these layers.
Joe R
+1  A: 

1a) is correct. 1b) is sort of correct; you may let the exception go up the stack from your model/business layers to the presentation, and display an error message. It doesn't have to go all the way to the Application_Error; Page_Error may be good, but some exceptions might be (relatively) common enough that, while you can not gracefully recover, you should have specific error messages related to them in some contexts.

2) basically; and also as a 'catch all' for exceptions you have not been able to predict and catch in more specific places.

Andrew Barber
1. “Application_Error; Page_Error may be good, but some exceptions might be (relatively) common enough that, while you can not gracefully recover, you should have specific error messages related to them in some contexts.” - A - I assume such exceptions are serious enough for page to not being able to recover gracefully ( but we’d still like the requested page to load normally with all its controls ), but not serious enough for user to be redirected to the error page? - B - Could you give an example of such exceptions?
2 – “It doesn't have to go all the way to the Application_Error; Page_Error may be good,…” But as far as I know, only difference between Application_Error and Page_Error is that Page_Error returns you to the requested page ( which is empty, since instances of control are not created ) .So why would Page_Error be preferred?
I worded that very clumsily! I don't necessarily think Page_Error is preferred for catching otherwise unhandled exceptions. In both cases I think you are down to just logging and returning a generic error message to the user. Also, about 'serious enough' errors that shouldn't just be shoveled off to a generic error page; something where it is logical that the error condition would be temporary, and the user should try again, perhaps including instructions to the user to fix it. Validation helps prevent those, but they can still happen.
Andrew Barber
thank you all for your help
+2  A: 

The main thing you want to avoid is silently swallowing exceptions that cannot be recovered from. (In short, having a try...catch block where you display some error to the user, but don't rethrow the exception, where thereby only the end user is aware that something went awry.)

If an exception happens and you can recover from it, great!

If an exception happens and you can't recover from it, then it is important that the exception details be logged and that the developers are notified of the exception. Typically this is done by letting the exception propagate to the ASP.NET runtime layer so that ASP.NET's Error event will fire. It's a best practice to subscribe to this Error event in some manner so as to log the error details and notify developers. One such tool for logging and notifying in the face of an error is ELMAH, another is the ASP.NET Health Monitoring library.

Also, if I may, I'd suggest this article of mine, which covers the points I made in here more depth and detail: Exception Handling Advice for ASP.NET Web Applications.

Happy Programming!

Scott Mitchell
"Typically this is done by letting the exception propagate to the ASP.NET runtime layer so that ASP.NET's Error event will fire. It's a best practice to subscribe to this Error event in some manner"So we shouldn’t handle them inside Application_Error or Page_Error, but instead subscribe to Error event?