Catching exceptions thrown from threads?

A

Alex

Hi

Is it possible to catch exceptions thrown by a System.Threading.Thread
object?

i.e. I have an instance of a class that is created using the normal
Thread/ThreadStart method, but once it starts, there will be no try/catch
block to catch any exceptions thrown (other than the ones that might be
raised by the Thread/ThreadStart classes when I'm initiating the thread).

I was thinking of using events (i.e. instead of throwing an exception my
class can raise an event), but that still won't ensure all exceptions are
trapped.

Thanks
Alex
 
D

Daniel O'Connell [C# MVP]

Alex said:
Hi

Is it possible to catch exceptions thrown by a System.Threading.Thread
object?

If you are using Windows Forms you should be able to hook
Application.ThreadException event to handle unhandled exceptions thrown from
threads.
 
W

William Stacey [MVP]

Put a try/catch around your whole thread start method. Then log or report
exception back to where ever as needed using event or your own queue.

--
William Stacey [MVP]

| Hi
|
| Is it possible to catch exceptions thrown by a System.Threading.Thread
| object?
|
| i.e. I have an instance of a class that is created using the normal
| Thread/ThreadStart method, but once it starts, there will be no try/catch
| block to catch any exceptions thrown (other than the ones that might be
| raised by the Thread/ThreadStart classes when I'm initiating the thread).
|
| I was thinking of using events (i.e. instead of throwing an exception my
| class can raise an event), but that still won't ensure all exceptions are
| trapped.
|
| Thanks
| Alex
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top