Fatal exception handling in C #?

How can I execute some code when my program experiences a fatal error and crash? For example, something is wrong, and a pop-up window appears that says: "TestApp.exe has encountered an error and should close." and then I want to write to the file with the error code and say a report about the last few things that were entered into the program. How would this be done in C #?

+4
source share
4 answers

Depending on the type of application you are writing, there are different ways. You can take a look at the following article to find out how this can be achieved in a WinForms application.

+3
source

It is usually a bad idea to write handlers for Exception , FatalException and two other types, you should be more specific in the try.. catch section. Try to study the Application class, especially its SetUnhandledExceptionMode . You can find a good example of how to use it here .

0
source

It depends on what you ask here.

Regular exceptions that you can get through try / catch.

However, if you use any external dll that can, for example, get corrupted status exceptions (CSE), you need to enable legacyCorruptedStateExceptionsPolicy in your app.config (but do not forget that Microsoft should delete this default value before You should try to correct such errors, so as not only to catch and forget them)

http://dotnetslackers.com/articles/net/All-about-Corrupted-State-Exceptions-in-NET4.aspx

0
source

You can use log4net to log errors in a file

In any case, it should look like this:

 try { // Something potentially dangerous } catch (Exception Ex) { Logger.Append(Ex.Message); } 
0
source

Source: https://habr.com/ru/post/1379945/


All Articles