Mike Marks Mike Marks - 4 months ago 27
ASP.NET (C#) Question

Will OnError in an HttpModule supersede any existing existing error handling?

I have been tasked with creating a sort of global error handling solution, so I'm creating an

HttpModule
to be dropped into an existing ASP.NET solution. Sometimes these existing web apps will have their own error handling (try/catch) that might do something specific.

QUESTION When an app encounters an exception, does the
OnError
in my
HttpModule
fire last since it has more of a global scope than a specific try/catch inside the app? Will it fire last every single time? Will
OnError
catch every single application exception, handled AND unhandled? When I say handled I mean if an error is already "handled" in some section of my code somewhere in my app, will it wind up in
OnError
?

Answer

Your HttpModule will only get those exceptions that are not caught by the application, as is clearly stated in the documentation

If the code that throws an exception is inside a try...catch block, there is no exception to be propagated to the global error handler.

If you handle the exception and want to supress the default ASP.NET error message you can do so with the ClearError method.

If you really want to know about all exceptions, you can use the FirstChanceException event on the AppDomain to get notify directly when the exception is caught. Not however that you will probably catch way more exceptions than you expect that way - many exceptions are thrown and gracefully handled within the framework.