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

Posted on 2012-08-23
Last Modified: 2012-09-02

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

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

Question by:jxbma
    LVL 23

    Expert Comment

    Unless you are using threads you can use something like this

    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler((o, e) => { 
    // 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" +

    Open in new window

    LVL 1

    Author Comment

    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)?

    LVL 23

    Accepted Solution

    This link has some information on handling wpf exceptions at a global level


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    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.…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now