none
what's the best way to handle global exceptions in WPF applications?

    Question

  • Hi All,

    As the question title, what's then the best way to handle global exceptions in WPF applications?

    Any note will be appreciated!

     

    Thanks.

     

    Wednesday, November 21, 2007 2:53 AM

Answers

  • Hi,

    Please take a look at the Application.DispatcherUnhandledException event. That's what you are looking for, I think.

    http://msdn2.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx

    Best regards,

    Benny
    Wednesday, November 21, 2007 8:15 AM
  • Also see AppDomain.UnhandledException.  Application.DispatcherUnhandledException is only raised if the exception was raised on the UI thread, whereas AppDomain.UnhandledException is raised for exceptions that occur on background threads. Typically I attach to both and run similar handling code. The only difference is that DispatcherUnhandledException allows you to "recover" by setting Handled to true. In other words, you could prompt your user to see whether they'd like to attempt to continue running (and potentially avoid data loss).

    HTH,
    Kent
    Wednesday, November 21, 2007 4:08 PM

All replies

  • Hi,

    Please take a look at the Application.DispatcherUnhandledException event. That's what you are looking for, I think.

    http://msdn2.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx

    Best regards,

    Benny
    Wednesday, November 21, 2007 8:15 AM
  • Also see AppDomain.UnhandledException.  Application.DispatcherUnhandledException is only raised if the exception was raised on the UI thread, whereas AppDomain.UnhandledException is raised for exceptions that occur on background threads. Typically I attach to both and run similar handling code. The only difference is that DispatcherUnhandledException allows you to "recover" by setting Handled to true. In other words, you could prompt your user to see whether they'd like to attempt to continue running (and potentially avoid data loss).

    HTH,
    Kent
    Wednesday, November 21, 2007 4:08 PM
  • Thank you guys!

     

    and Kent, as you said I could subscribe AppDomain.UnhandledException and Application.DispatcherUnhandledException for the Background and UI thread exceptions.

     

    and for Application.DispatcherUnhandledException

    I can make 'e.Handled = true;'  'recover' the exception and make my Application continue to run.

     

    but for AppDomain.UnhandledException

    what's the e.IsTerminating for?

     

    Does it the samething with e.Handled?

     

    In the before, we can catch the global excetpions in a .Net Application easily through a global handled event.

    but now in WPF, we must handle the two?

    Is there any another way like before?

     

    Thanks again for your help!

     

     

    Thursday, November 22, 2007 1:35 AM
  • From MSDN:

    Beginning with the .NET Framework version 2.0, this property returns true for most unhandled exceptions, unless an application compatibility flag is used to revert to the behavior of versions 1.0 and 1.1. The reason is that most unhandled exceptions in threads, including thread pool threads, are allowed to proceed naturally, which normally results in termination of the application. For more information, including a list of cases in which the runtime does not terminate, see Exceptions in Managed Threads.

    Also, Winforms has very similar facilities as WPF applications: there is a UI unhandled exception handler where you can opt to exit or continue the app, and there is the AppDomain.UnhandledException handler for exceptions in other threads.

    HTH,
    Kent
    Thursday, November 22, 2007 9:21 AM