views:

67

answers:

2

Is there a C# equivalent of Java's Thread.setDefaultUncaughtExceptionHandler()?

+6  A: 

There's AppDomain.UnhandledException for non-WinForms, and Application.ThreadException for WinForms.

They're unlikely to be exactly equivalent to the Java handler, but they may do what you need.

Jon Skeet
Noteworthy: "you'll get the standard .NET crash dialog in a console app, even if you've registered an unhandled exception handler for your AppDomain" http://www.codinghorror.com/blog/archives/000201.html
dtb
@dtb - Thanks, I just found that out. :-)
Taylor Leese
A: 

something like this ?

in your main method use this

Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

declare this

public static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
    ... code goes here ...
}

That should handle any thread exceptions in a win-forms app, for console take a look at the AppDomain handler detailed by the poster above.

krystan honour