Stock Photos, Royalty Free Stock Photography, Photo Search
Royalty Free Images

    Catching Unhandled Exception in WPF Application

    Oftentimes for troubleshooting purposes it is necessary to trap and report unhandled exceptions. This article will demonstrate how to accomplish this within a WPF application.

    The default behavior of a WPF application is to catch all unhandled exceptions, alert the user that the software has encountered a fatal error (shown below), and then shut the application down.


    Note that in a fully featured production system, rather than display a custom error screen (or log exceptions to a data file) it is possible/desirable to use Windows Error Reporting to analyze errors within your application. When the user presses the “Send Error Report” button Microsoft captures the error report, which you can then view (using a secure account) to identify problem areas.

    To display an error report directly within your WPF application, you can trap all unhandled exceptions using the Application.DispatcherUnhandledException event. This is done by modifying the App.xaml and App.xaml.cs files.

    In App.xaml, the registration of the event handler is initialized (highlighted below).


    A method is added to App.xaml.cs which will handle this event. This is where the exception handling logic will be added.


    DispatcherUnhandledException will only handle exceptions thrown on the UI thread. In multithreading applications, you must ensure that exceptions thrown on secondary threads are caught and re-thrown (or handled) on the UI thread. Unfortunately, there is no built-in mechanism for this behavior.

    The sample application (source here) displays a window containing a button which fires an exception. The DispatcherUnhandledException event handler is used to display an “Exception Viewer” window, which displays the details of the exception.



    As a best practice, you typically do not want to show the end-user the technical details of an exception. A more robust system of error reporting could be implemented that would securely report exception details, while the end-user would only see a dialog that an error has been encountered.

    Another alternative is that you could use Windows Error Reporting for deployed solutions, which could be disabled in favor of custom error reporting for internal or development (beta) systems.

    Trini wrote on 8/16/2013 4:10:53 AM:

    Wonderful paintings! That is the kind of irtnomafion that are supposed to be shared across the internet. Shame on the seek engines for no longer positioning this post higher! Come on over and visit my web site . Thank you =)

    Yanira wrote on 9/3/2013 9:34:47 PM:

    I'm extremely imserpsed along with your writing abilities and also with the format in your blog. Is this a paid subject or did you modify it your self? Either way stay up the excellent quality writing, it's rare to see a great blog like this one these days.. [url=]tcujiefck[/url] [link=]kjucpulzyxj[/link]

    Michal wrote on 12/21/2015 2:10:36 AM:

    Usually I do not read article on blogs, but I wish to say that this write-up very clmeploed me to take a look at and do it! Your writing taste has been surprised me. Thanks, quite nice post.

    Osiris wrote on 12/22/2015 5:08:56 PM:

    Unfortunately I did not record this video nor do I host it. It is being seervd from UserGroup.TV, which I do not control. Can you follow the link to the original post and see if you can view it there? [url=]oybqiect[/url] [link=]cntghm[/link]

    Leave a Comment

    Enter CodeEnter security code