I am building a multi-layered application that has an ASP.NET MVC web application. It conists of the usuals like presentation layer, business layer, data layer, etc. How would one create/use a decent exception handling mechanism? I read on Patterns and Practices that you need to bubble up exceptions to the various layers.
Also logging. Where does logging take place? In the MVC web application?
How would you redirect to various error pages depending on the type of error?
I would appreciate some feedback regarding this, and some articles if you guys have some. If there are any sample apps that make use of a decent exception hadling and logging strategy please let me know :)
Logging and exception handling are like two peas in a pod. When a problem happens in your Java code, that typically means you have an exception that needs to be handled, and of course, any time an error or unanticipated event occurs, that occurrence should be logged appropriately.
Exception handling is a mechanism in which a programming construct is used to consistently trap, intercept and handle the error occurred during application execution.
An exception handling strategy consists of all actions and conventions necessary to properly handle exceptions in your application. The following diagram shows an overview of the various parts of an exception handling strategy, from the error is detected, all the way up to where the error is handled.
First, I would suggest reading the article "Vexing Exceptions" by Eric Lippert. This should give you some reasonable guidance on exception-handling (and more on exception throwing).
When it comes to exception logging, the easiest and cleanest approach is to have a "top-level" exception handler responsible for dealing with all otherwise unhandled exceptions and record them to a log for analysis. This can be done in ASP.NET applications through the HttpApplication.Error
event which you can hook into through your Global.asax file.
If a part of the application is trapping an error (catching it) and handling it in some way, it may be appropriate to log a warning or informational at that point, so can record a problem occurred, but that the system dealt with it. Try to avoid filling your code with these though, as they can quickly become a maintenance problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With