• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1133
  • Last Modified:

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
0
jxbma
Asked:
jxbma
  • 2
1 Solution
 
Michael FowlerSolutions ConsultantCommented:
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

0
 
jxbmaSoftware ConsultantAuthor Commented:
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
0
 
Michael FowlerSolutions ConsultantCommented:
This link has some information on handling wpf exceptions at a global level

http://stackoverflow.com/questions/1472498/wpf-global-exception-handler

Michael
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now