Is there any way to handle all software exceptions?


I am creating a DLL for use in many of my projects. I've created a method that captures Source, Message, and other variables I've set, mounts an email, and sends it to myself. (a kind of error log by email).

Is there any way to reference this method once in the code, and does it catch every time a Exception is cast even without Try... Catch ?

asked by Kaizonaro 30.01.2015 в 16:14

2 answers


You can add an event handler to AppDomain.UnhandledException , this event will fire when an exception is not detected / handled.


It allows the application to log information about the exception   before the system's default handler reports the exception to the   and quit the application. If sufficient information   state of the application is available, other measures can be taken   - how to save program data for later recovery.


Caution is advised because the program data can be   corrupted when exceptions are not handled.

Example taken from here in C #.

Add a line to the main method (by default in the Program.cs file in a new Windows application):

namespace MyApp
  static class Program
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
      AppDomain.CurrentDomain.UnhandledException +=  new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
      Application.Run(new Form1());

At line AppDomain.CurrentDomain... you are referencing a function that does not yet exist, so we will create it:

static void CurrentDomain_UnhandledException (object sender, UnhandledExceptionEventArgs e)
    Exception ex = (Exception)e.ExceptionObject;    
    MessageBox.Show("Erro! Entre em contato com os desenvolvedores com a seguinte" 
          + " informação:\n\n" + ex.Message + ex.StackTrace, 
          "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);

Now your unhandled exceptions are being displayed in a nice dialog, you can do other things around here - such as logging the exception, or trying to smooth out the crash, you can not, however, keep the program running after a < in> crash , there is no way to catch the exception at this point and let the program work.

In VB.NET should look similar to this:

Module MyApp   
   Sub Main()
      Dim currentDomain As AppDomain = AppDomain.CurrentDomain
      AddHandler currentDomain.UnhandledException, AddressOf MyHandler

      Throw New Exception("Foo")
   End Sub 

   Sub MyHandler(sender As Object, args As UnhandledExceptionEventArgs)
      Dim e As Exception = DirectCast(args.ExceptionObject, Exception)

      Console.WriteLine("Erro! Entre em contato com os desenvolvedores com a seguinte : " + e.Message)
      ' Fazer alguma coisa aqui com a exceção não tratada.
   End Sub     
End MyApp
30.01.2015 / 16:26

Yes, there is. First there are some things I want to tell you.

With some exception (with pardoning the pun) well justified there is only one place where you should capture Exception . It is near the end of the application. Often in Main() or some method called by it. This is where you must capture all the exceptions that were thrown and not captured before. That's where you should do what you indicated. And almost always the application should break even, it may even be in a "cute" way but it is the best to do when the problem can not be fixed. And programming errors can not be fixed at runtime. The most you can do to keep from breaking is to start over with a zeroed state. The application would not break but would start all over again as if it were the first run. So it should be in Main() or close to it.

But if you really want to capture something that has not even been captured there is a way:

   'isto possivelmente vai no Main
   Dim currentDomain = AppDomain.CurrentDomain
   currentDomain.UnhandledException += new unhandledExceptionEventHandler(MyHandler)

   Shared Sub MyHandler(object sender, UnhandledExceptionEventArgs args) {
      Dim e =  DirectCast(args.ExceptionObject, Exception)
      Console.WriteLine("Erro: {0}", e.Message)
      'aqui você pode fazer o que quiser
   End Sub

Documentation .

I advise avoiding this method, this may not do what you expect.

30.01.2015 в 16:30