Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to catch all unhandled exceptions in C#.NET

Tags:

c#

asp.net

I have a website built in C#.NET that tends to produce a fairly steady stream of SQL timeouts from various user controls and I want to easily pop some code in to catch all unhandled exceptions and send them to something that can log them and display a friendly message to the user.

How do I, through minimal effort, catch all unhandled exceptions?

this question seems to say it's impossible, but that doesn't make sense to me (and it's about .NET 1.1 in windows apps):

like image 454
adambox Avatar asked Nov 17 '08 14:11

adambox


People also ask

How do you find the unhandled exception?

If your application has unhandled exceptions, that may be logged in the Windows Event Viewer under the category of “Application”. This can be helpful if you can't figure out why your application suddenly crashes. Windows Event Viewer may log 2 different entries for the same exception.

Can you catch exceptions in C?

C itself doesn't support exceptions but you can simulate them to a degree with setjmp and longjmp calls.

Does exception catch all exceptions?

Since Exception is the base class of all exceptions, it will catch any exception.

How do you catch different types of exceptions?

Exception handling is used to handle the exceptions. We can use try catch block to protect the code. Catch block is used to catch all types of exception. The keyword “catch” is used to catch exceptions.


2 Answers

All unhandled exceptions finally passed through Application_Error in global.asax. So, to give general exception message or do logging operations, see Application_Error.

like image 101
Ali Ersöz Avatar answered Sep 22 '22 11:09

Ali Ersöz


If you need to catch exeptions in all threads the best aproach is to implement UnhandledExceptionModule and add it to you application look here for an example

like image 24
MichaelT Avatar answered Sep 23 '22 11:09

MichaelT