Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch unhandled exceptions in an asp.net application?

Tags:

.net

asp.net

On a .NET exam I encountered this question.

Which of the following can you use to catch unhandled exceptions in an application?

  • Server_Error
  • Page_Error
  • Application_Error
  • Response_Error
  • OnError event

I know its Application_Error, But what I want to know is what are the others. By some google search I found OnError can be used to catch any errors. Still I am not sure about it. Could you tell what are the other ways to catch unhandled exception

like image 497
Shiplu Mokaddim Avatar asked Feb 21 '23 05:02

Shiplu Mokaddim


1 Answers

The correct point to catch your unknown errors is the Application_Error.

Avoid to capture the OnError on your page, and let the system transfer it to the Application_Error because there you have lost the control on the page, so what other you can do if not transfer it to some error page ? - if you try to just reload it you have the problem to be in a close loop that can cause stack overflow.

From my experience I have issue when I try to handle errors using the page OnError and I use it only when I have to free some global memory, or something like that if an error happens on page.

To give a summary, try to catch all your errors inside a try/catch block, and give a message to your user / or just take care of this issues, but let the unknown errors to the global catcher to log it and fix it. The unknown errors is something that make you lose the real control of your program, and actually you do not know what to do because you do not have predict it - so log it and fix it for the next time

More details about the errors : How do I make a "generic error" page in my ASP.NET application so that it handles errors triggered when serving that page itself?

like image 53
Aristos Avatar answered Mar 06 '23 21:03

Aristos