Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

throw an Exception in thread C#

I have thread Like this, after i see example link text

ThreadStart _threadStart = new ThreadStart(delegate()
{
       try
       {
           threadFunction(httpContext);
       }
       catch (Exception ex)
       {
           throw ex;
       }
 });
 Thread _thread = new Thread(_threadStart);
  _thread.Start();

when an Exception happen it dosen't re-thrown in the thread that started it. So what i'm doing wrong or how to do it?

Note: thanks to all comments in advanced

like image 573
Hiyasat Avatar asked Jan 23 '11 13:01

Hiyasat


2 Answers

The exception will be thrown, but that will only end the thread. The exception isn't re-thrown in the thread that started it.

like image 106
Guffa Avatar answered Nov 07 '22 08:11

Guffa


I think the heart of the matter is to understand that exceptions that occur within a thread will not be passed to the calling thread for handling.

For example, say you have a rebel method:

private static void RebelWithoutACause()
{
    throw new NullReferenceException("Can't touch this!");
}

Let's say you create a new thread that calls this method in your program, and being a safe programmer, you decide to envelope the work in a try/catch block:

private static void Main(string[] args)
{
    try
    {
        var thread = new Thread(RebelWithoutACause);
        thread.Start();
        thread.Join();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

However, if you run this in the debugger, you will find out that you'll never get to the catch block, and instead the thread will be killed, and the debugger will complain that you have an unhandled exception.

You need to take your pick of how to handle the exceptions, but the handling needs to happen inside each thread entry method. Typical handling includes logging the details, notifying users via the UI, and shutting down your application as gracefully as you can.

like image 35
Gustavo Mori Avatar answered Nov 07 '22 08:11

Gustavo Mori