Catch Exception thrown by spawned Thread.Start in Parent Thread

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a program with the following lines:

Thread thread = new Thread(new ThreadStart(Execute));
thread.Start();

I want to be able to handle any exceptions that are thrown by execute that
aren't handled in the execute method in the code that spawned the thread. In
other words, I would like to write the following:

try{
thread.Start()
}
catch{
//Handle any exception thrown by thread.start so my program doesn't crash
}

However, when I write this code, if Execute throws an exception, it bubbles
to the top of the thread and then crashes the entire program. Is there a way
to catch an exception from the thread so my program doesn't crash? Thanks
 
Hi

I think you may try to use the AppDomain's UnhandledExceptionEventHandler
to see if that works for you.
Commonly the new thread exit abnormal will not make the main thread
exit.(Crash);
static void ThreadProc()
{
throw new Exception("Thread Exception");
}
[STAThread]
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException+=new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Thread th = new Thread(new ThreadStart(ThreadProc));
th.Start();
}

private static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
Console.WriteLine("Enter unhandled Exception");
Console.WriteLine(e.ExceptionObject.ToString());
}

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
I tried using the code suggested above. However, if I understand its purpose,
it's only meant to be aware of any exceptions that are thrown in the
AppDomain, but it can't actually catch it.

In other words, with this code if the spawned thread throws an exception
that isn't handled anywhere in the spawned thread code, it still will crash
my application although it does notify me of the exception before crashing. I
need something that can actually catch that exception in the code that
spawned the thread so that the main application doesn't crash.

Thanks
 
Hi

I think we have two points to clarify.
1. common the spawned thread throw unhandled the exception the spawned
thread will terminated, but it will not crash the main applicaiton.
You will find that even if we did not do any thing with the exception
thrown in ThreadProc, the Main Thread is still alive.
static void ThreadProc()
{
Console.WriteLine("Enter Thread");
throw new Exception("Thread Exception");
}
[STAThread]
static void Main(string[] args)
{
//AppDomain.CurrentDomain.UnhandledException+=new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Thread th = new Thread(new ThreadStart(ThreadProc));
th.Start();
for(int i=0;i<10;i++)
{
Console.WriteLine("Main Thread alive...");
Thread.Sleep(3000);
}

}

private static void CurrentDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
Console.WriteLine("Enter unhandled Exception");
Console.WriteLine(e.ExceptionObject.ToString());
}

2. In OS Thread design, all the code is running by thread. All the
exception/Call stack is based on certain detailed thread, one thread can
not catch the exception on another thread.

So for your scenario, you may try to put try...catch inside your threadproc.
static void ThreadProc()
{
try
{
Console.WriteLine("Enter Thread");
throw new Exception("Thread Exception");
}
catch{}
}

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Dave said:
I tried using the code suggested above. However, if I understand its purpose,
it's only meant to be aware of any exceptions that are thrown in the
AppDomain, but it can't actually catch it.

In other words, with this code if the spawned thread throws an exception
that isn't handled anywhere in the spawned thread code, it still will crash
my application although it does notify me of the exception before crashing. I
need something that can actually catch that exception in the code that
spawned the thread so that the main application doesn't crash.

Thanks

You can only catch the exception in the thread that generates it. If
you do not have control over the Execute function, then I'd recommend
creating a small function which wraps around Execute, and starting the
thread on that function instead. The whole point of threading is that
things happen in parallel. So if/when your second thread throws an
exception, you have no idea where execution is in your main thread.

Damien
 
Hi

Did my suggestion help you?
If you still have any concern, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
You need to put a try-catch within the Execute method itself...the only way
you can catch an exception thrown on a specific thread is to catch it on
that thread. If you cannot do that then all you can do is receive a UE
notification that an unhandled exception occurred, after which it will
either be ignored or the app will be terminated (depending on the thread and
the version of the runtime).
 
Back
Top