Exception handling in a three-tier Asp.Net applica

2020-07-27 03:16发布

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

3条回答
Animai°情兽
2楼-- · 2020-07-27 03:36

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.

查看更多
成全新的幸福
3楼-- · 2020-07-27 03:56

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!

查看更多
▲ chillily
4楼-- · 2020-07-27 04:02

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.

查看更多
登录 后发表回答