Link to home
Start Free TrialLog in
Avatar of jxbma
jxbmaFlag for United States of America

asked on

How do I handle unhandled Application level exceptions in a WPF application?

Hi:

I'm migrating an existing Winforms based application to WPF.

For handling unhandled exceptions at the application level in my Winforms app, I did the following:

// Added for application level error handling
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);    
// This is a delegate!  That is a user method inside the parens
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);  
// This is a delegate!  That is a user method inside the parens
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);


How do I go about handling these conditions in a WPF based application?

Thanks,
JohnB
Avatar of Michael Fowler
Michael Fowler
Flag of Australia image

Unless you are using threads you can use something like this

Michael
         
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler((o, e) => { 
            UnhandledException((Exception)e.ExceptionObject);

// Unhandled exceptions
static void UnhandledException(Exception e) {
   System.Windows.MessageBox.Show("An error has occurred and the program will now exit" + "\r\n" +
                                                            "Error Message:" + "\r\n" +
                                                            e.Message + "\r\n\r\n" +
                                                            e.StackTrace);
   Application.Exit();
 }

Open in new window

Avatar of jxbma

ASKER

OK. This is what I was doing in my Winforms app.
How would I handle exceptions thrown within threads (either my own, or spun from 3rd party libraries)?

Thanks,
JohnB
ASKER CERTIFIED SOLUTION
Avatar of Michael Fowler
Michael Fowler
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial