Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2012-08-23
3
Medium Priority
?
1,080 Views
Last Modified: 2012-09-02
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
Comment
Question by:jxbma
  • 2
3 Comments
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 38327987
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
 
LVL 1

Author Comment

by:jxbma
ID: 38329493
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
 
LVL 23

Accepted Solution

by:
Michael Fowler earned 2000 total points
ID: 38331760
This link has some information on handling wpf exceptions at a global level

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

Michael
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question