Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch ALL exceptions/crashes in a .NET app [duplicate]

Possible Duplicate:
.NET - What’s the best way to implement a “catch all exceptions handler”

I have a .NET console app app that is crashing and displaying a message to the user. All of my code is in a try{<code>} catch(Exception e){<stuff>} block, but still errors are occasionally displayed.

In a Win32 app, you can capture all possible exceptions/crashes by installing various exception handlers:

/* C++ exc handlers */ _set_se_translator SetUnhandledExceptionFilter _set_purecall_handler set_terminate set_unexpected _set_invalid_parameter_handler 

What is the equivalent in the .NET world so I can handle/log/quiet all possible error cases?

like image 832
DougN Avatar asked Sep 17 '08 12:09

DougN


People also ask

How would you catch multiple exceptions at once in C#?

In C#, You can use more than one catch block with the try block. Generally, multiple catch block is used to handle different types of exceptions means each catch block is used to handle different type of exception.

Does throwing an exception crash app?

Your exception never gets caught, so that's why your application is crashing. Because your exception is extended from the RuntimeException class the default behaviour is to exit the application if the exception is not catched anywhere. So that's why you should catch it before the Java Runtime decides to quit the app.

What is an exception how exceptions are handled in ASP NET application?

Exception class. An exception is thrown from an area of code where a problem has occurred. The exception is passed up the call stack to a place where the application provides code to handle the exception. If the application does not handle the exception, the browser is forced to display the error details.

How exception is handled globally in C#?

An ExceptionFilterAttribute is used to collect unhandled exceptions. You can register it as a global filter, and it will function as a global exception handler. Another option is to use a custom middleware designed to do nothing but catch unhandled exceptions.


1 Answers

You can add an event handler to AppDomain.UnhandledException event, and it'll be called when a exception is thrown and not caught.

like image 116
Juanma Avatar answered Nov 06 '22 15:11

Juanma