Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CLR exits when a thread gets unhandled exception

I have background threads in my application. When one of the threads gets an exception that is not handled the entire CLR exits.

Is it normal behavior, or is it a bug in the CLR?

I would expect that the thread will exit but the CLR will continue working.

like image 356
ofer alper Avatar asked Jan 30 '11 15:01

ofer alper


People also ask

How does CLR handle exception?

Exception, the CLR execution engine can raise exceptions, and unmanaged code can raise exceptions as well. Exceptions raised on a thread of execution follow the thread through native and managed code, across AppDomains, and, if not handled by the program, are treated as unhandled exceptions by the operating system.

How do you handle an unhandled exception in the thread?

Uncaught exception handler will be used to demonstrate the use of exception with thread. It is a specific interface provided by Java to handle exception in the thread run method. There are two methods to create a thread: Extend the thread Class (java.

What happens when an exception occurs in a thread?

If an unhandled exception occurs in the main thread of an application, the main thread terminates, along with your entire application. An unhandled exception in a spawned worker thread, however, will terminate only that thread.

Does an exception terminate thread?

If any of these exceptions are unhandled in threads created by the common language runtime, the exception terminates the thread, but the common language runtime does not allow the exception to proceed further.


2 Answers

The default behavior in .NET applications is to exit whenever an unhandled exception occurs. When an exception goes unhandled, the program is in an unknown and possibly unsteady state. Just because it happened in a background thread doesn't mean that the error won't affect the rest of the program. The most prudent course for the runtime in that situation is to dump the program.

You might look into AppDomain.CurrentDomain.UnhandledException, which will allow you to catch unhandled exceptions and react accordingly. A better solution is to wrap your thread proc with a try...catch. But only have it handle those exceptions it knows how to handle. Doing this:

void MyThreadProc()
{
    try
    {
        // ... 
    }
    catch
    {
        // handle all exceptions
        // This is a BAD idea
    }
}

Is a really bad idea, because it can mask exceptions that you really do want to be propagated to the main program.

like image 66
Jim Mischel Avatar answered Sep 28 '22 01:09

Jim Mischel


Your expected behavior used to be the behavior back in 1.1. It was generally considered to have been a bad idea. When you have an unhandled exception in any thread, your process can be left in an inconsistent state. Updates to shared data may be partially applied, etc. The runtime doesn't have the information to safely handle this scenario, or even know how you want to handle this scenario, so its choice would amount to terminating the thread and leaving your program in a strange state. This could lead to resource leaks, hangs, corruption of data, etc. By terminating the process given an unhandled exception you know exactly what happens, the process ends.

like image 21
Logan Capaldo Avatar answered Sep 28 '22 01:09

Logan Capaldo