C# Exception in thread

  • Thread starter Thread starter ng5000
  • Start date Start date
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
 
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.
 
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
 
Back
Top