Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching Exceptions in a .NET Thread

How could you find out that an Exception occurred in a Thread in a MultiThreaded Application ? and consecutively clean the resources ?

Because otherwise the Thread can be still remaining in memory and running.

like image 759
theSpyCry Avatar asked Mar 17 '10 10:03

theSpyCry


People also ask

How do you catch an exception in a thread?

Exceptions are caught by handlers(here catch block). Exceptions are caught by handlers positioned along with the thread's method invocation stack. If the calling method is not prepared to catch the exception, it throws the exception up to its calling method and so on.

What happens if a thread throws an exception C#?

This event provides notification of uncaught exceptions. It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application.

Can we catch exceptions thrown by another thread?

Yes, it can be done by using Thread. UncaughtExceptionHandler. When a thread is about to terminate due to an uncaught exception the Java Virtual Machine will query the thread for its UncaughtExceptionHandler usingThread.

What happens if there is exception in thread?

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.


2 Answers

As Sean has said, you have to do all exception handling and cleanup inside the thread method, you can't do it in the Thread initialization. For example:

public void Run()
{
    try
    {
        Thread thread1 = new Thread(ThreadEntry1);
        thread1.Start();

        Thread thread2 = new Thread(ThreadEntry2);
        thread2.Start();
    }
    catch (NotImplementedException)
    {
        // Neither are caught here
        Console.WriteLine("Caught you");
    }
}

private void ThreadEntry1()
{
    throw new NotImplementedException("Oops");
}

private void ThreadEntry2()
{
    throw new NotImplementedException("Oops2");
}

Instead, this approach is more self-contained and obviously also works:

public void Run()
{
    Thread thread1 = new Thread(ThreadEntry1);
    thread1.Start();
}

private void ThreadEntry1()
{
    try
    {
        throw new NotImplementedException("Oops");
    }
    catch (NotImplementedException)
    {
        Console.WriteLine("Ha! Caught you");
    }
}

If you want to know if the Thread has failed, then you should consider an array of WaitHandles, and signal back to your calling method. An alternative and simpler approach is to simply increment a counter each time a thread's operation finishes:

Interlocked.Increment(ref _mycounter);
like image 73
Chris S Avatar answered Oct 05 '22 10:10

Chris S


If you're worried about this sort of thing then you should wrap your threads entry point in a try/catch block and do the cleanup explicitly. Any exception passing out of the thread entry point will cause your app to shut down.

like image 41
Sean Avatar answered Oct 05 '22 10:10

Sean