PC Review


Reply
Thread Tools Rate Thread

Catch Exception thrown by spawned Thread.Start in Parent Thread

 
 
=?Utf-8?B?RGF2ZSBCb29rZXI=?=
Guest
Posts: n/a
 
      15th Nov 2005
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





 
Reply With Quote
 
 
 
 
Peter Huang [MSFT]
Guest
Posts: n/a
 
      16th Nov 2005
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.

 
Reply With Quote
 
=?Utf-8?B?RGF2ZSBCb29rZXI=?=
Guest
Posts: n/a
 
      16th Nov 2005
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
 
Reply With Quote
 
Peter Huang [MSFT]
Guest
Posts: n/a
 
      17th Nov 2005
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.

 
Reply With Quote
 
Damien
Guest
Posts: n/a
 
      17th Nov 2005

Dave Booker wrote:
> 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

 
Reply With Quote
 
Peter Huang [MSFT]
Guest
Posts: n/a
 
      21st Nov 2005
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.

 
Reply With Quote
 
David Levine
Guest
Posts: n/a
 
      22nd Nov 2005
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).


"Dave Booker" <(E-Mail Removed)> wrote in message
news:0D2CEBFC-E3BA-4F69-82C8-(E-Mail Removed)...
>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
>
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Start a thread with the same credentials as the parent thread =?Utf-8?B?cGxleDRy?= Microsoft ASP .NET 0 13th Nov 2007 08:11 PM
Threading - Merging a spawned thread with the main thread =?Utf-8?B?TmVkcw==?= Microsoft Dot NET 1 12th Feb 2005 09:18 AM
How do I catch an exception occurred in the work thread from the main thread Kueishiong Tu Microsoft Dot NET Framework Forms 6 1st Dec 2003 10:56 PM
How to catch an exception ocurred in the work thread from the main thread? Kueushiong Tu Microsoft Dot NET Framework 1 30th Nov 2003 08:57 AM
Call the dispose method on a different thread if an exception is thrown Giovanni Bassi Microsoft VB .NET 3 16th Oct 2003 05:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:01 PM.