Global Catching of All Unhandled Exceptions

N

NvrBst

Hello. I was wondering if its possible? Basically my program has a ThreadPool and that ThreadPool
works on many different methods. Its very important to me that no pop up's ever occur; I have a
"try/catch" block around my main but any exception that the ThreadPool creates crash's the
application (and produces a pop up). ((For example Java has the "unhandledException(...)" method in
the "ThreadGroup" class that you can simply override)). Does C# have something like this?

Things I've Tried:
http://msdn.microsoft.com/library/default.asp?url=/library/en-
us/cpref/html/frlrfsystemwindowsformsapplicationclassthreadexceptiontopic.asp

This has almost exactly what I wanted (I thought...). I add the following to my main (and the
class)

CustomExceptionHandler eh = new CustomExceptionHandler();
Application.ThreadException += new ThreadExceptionEventHandler(eh.OnThreadException);

internal class CustomExceptionHandler {
public void OnThreadException(object sender, ThreadExceptionEventArgs t) {
MessageBox.Show("IsThisWorking?");
Application.Exit();
}
}

But then I have the ThreadPool invoke a method that simply throws an excpetion and it still crash's
my program... No MessageBox is shown either. Can someone tell me what I'm missing?


Some more information: Basically I know I can put "try/catch" blocks around all the methods that
the ThreadPool can invoke on but it just seems ugly and theres a lot of methods (this is what I am
currently doing). Also I don't care if I can post any messages to the user or anything when an
uncaught exception occures (simply having the application exit would be great so if there's a way
for my program to just disable the "XXXX has encountered a problem and needs to close. We are sorry
for the inconvenience." Then that would also be a great solution for me if someone knows how I can
set my program to do that...


Best Regards,
Never Best
--
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Sure do, you can use Application.ThreadException or
AppDomain.UnHandledExeption


Cheers,
 

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