C# Exception in thread

N

ng5000

Hi,

I have a C# application. The application starts a thread. I have a
method that is run in the context of the thread. Sometimes (depends
how it's called etc.) the method catches, logs, and then throws an
exception.

The exception is then caught by the framework rather than my
application. I know this is bad design, and I intend to change the
application so that threads do not throw an exception from their last
catch statement. Is there, however, someway I can catch uncaught
thread exceptions?

I hope I was clear enough in my explanation but if not please ask.

Thanks,

Nick
 
N

Nicholas Paldino [.NET/C# MVP]

Nick,

You can always attach to the UnhandledException event on the current
AppDomain, and you will be notified when an unhandled Exception is thrown on
any thread.

However, an easier way to get around this would be to just have a shim
in between the ThreadStart delegate, and the actual delegate you want to
call. You would wrap the call to the delegate in a try/catch block, and
handle the case when something goes wrong.

Hope this helps.
 
D

David Lei

You can have a try..catch block within your thread to catch
the all Exceptions other than ThreadAbortException,
then forward the Exception back to the main thread
via Control.Invoke() method.

david
 

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