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.

    image

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

    image

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

    image

    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.

    image

    image

    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.. http://gdkuwsmiku.com [url=http://tcujiefck.com]tcujiefck[/url] [link=http://kjucpulzyxj.com]kjucpulzyxj[/link]

    Leave a Comment

    Name  
    Comment  
    Enter CodeEnter security code