Unhandled exception catching on secondary threads.

G

Guest

I wish to make sure all unhandled exceptions (UE) are handled by a central
handler.
For primary thread UEs, this may be done by

string exceptionText;

AppDomain.CurrentDomain.UnhandledException +=
delegate(object sender, UnhandledExceptionEventArgs e)
{
exceptionText = e.ExceptionObject.ToString();
};

However, this does not work for UEs thrown from a secondary thread like the
timer object (not the forms control).

In this case, the secondary thread is not known until the timer event fires.

Any ideas please?
 
M

Michael Bray

I wish to make sure all unhandled exceptions (UE) are handled by a
central handler.

While I'm not sure this deals with your question directly, there is one
thing you need to keep in mind when using AppDomain.UnhandledException.

AppDomain.UnhandledException is NOT a "handler" for the exception - it is
just something that the framework uses to notify you that an exception has
occurred.

I dealt with a similar issue recently as I was developing some code for
handling plugins. The details depend on which version of .NET you are
running. If I remember correctly, the following applies:

In .NET 1.1, exceptions thrown on other threads die away without causing
the application to die.

In .NET 2.0, exceptions thrown on other threads will cause the application
to die.

See the following: (watch the URL wrap)

http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx?
feedbackId=FDBK21092

A quote from the above URL: "As it turns out, the documentation of
UnhandledException is incorrect. (It's always been incorrect; I've filed a
bug to get it fixed.) The UnhandledException event handler is just a
notification that an exception has gone unhandled. Registering an event
handler does not actually handle the exception; it just handles the event."

-mdb
 

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

Top