Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching unhandled exception on separate threads

I am using the following event to catch unhandled exceptions in the main UI thread.

Application.ThreadException  

Unfortunately, it does not catch those unhandled errors in seperate threads. I am aware of

AppDomain.CurrentDomain.UnhandledException 

However, this seems to shut down the application upon triggering, where as the former does not.

Is there a way to deal with unhandled exceptions on separate threads, without the application closing?

like image 673
Darren Young Avatar asked Nov 26 '10 11:11

Darren Young


People also ask

How do you handle an unhandled exception in the thread?

you don't need to pass the exception from one thread to another. if you want to handle an exception, just do it in the thread which threw it. your main thread doesn't need to wait from the background thread in this example, which actually means you don't need a background thread at all.

Does an exception terminate thread?

If an exception is not caught with catch , the thread in which the exception occurred will be terminated. If no non-daemon threads remain the JVM will terminate. That's the only way how an exception might terminate a JVM. If you catch an exception it will never cause a JVM termination.

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.

What happens when an unhandled exception occurs?

An unhandled exception is an exception that does not have an associated handler. In C++ any unhandled exception terminates the program. It is unspecified whether the stack is unwound in this case, i.e. destructors of successfully constructed local variables may be executed or not depending on the compiler.


2 Answers

@Ani have already answered your question. Although I don't agree that unhandled exceptions in threads should terminate applications. Using threads usually means that you have some kind of server application. Bringing it down could result in a lot of angry users.

I've written a small piece about proper exception handling: https://coderr.io/exception-handling

You should always catch exceptions for threads. I usually use the following pattern:

  void ThreadMethod(object state)   {       try       {           ActualWorkerMethod();       }       catch (Exception err)       {           _logger.Error("Unhandled exception in thread.", err);       }   }    void ActualWorkerMethod()   {       // do something clever   } 

It's a whole lot easier to find thread methods that doesn't handle exceptions properly by moving the logic into a seperate method and just keep the try/catch block in the thread method.

like image 163
jgauffin Avatar answered Sep 22 '22 06:09

jgauffin


Of course you should always handle all exceptions. But if you are currently incapable of doing so, you can try the following:

The application will crash/close after the UnhandledException event handler. You can just add a delay in the event handler to prevents this. Other threads with no exception (e.g. the main thread) can continue. So the application will not close and can continue. However, the thread with the exception will remain in sleep. And therefor you may get a "memory/thread leak".

    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)     {         // Log the exception, display it, etc         Debug.WriteLine((e.ExceptionObject as Exception).Message);         Thread.Sleep(100000000);     } 

At this moment there is not a better solution. You may find to change the config file, but i think that is just as dirty: https://stackoverflow.com/a/15348736

like image 28
Maik van den Hengel Avatar answered Sep 21 '22 06:09

Maik van den Hengel