views:

1342

answers:

3

Hi,

sometimes, under not reproducible circumstances, my WPF application crashes without any message. The application simply close instantly.

Where is the best place to implement the global Try/Catch block. At least i have to implement a messagebox with: "Sorry for the inconvenience ..."

Any help would be most welcome, thank you.

+5  A: 

You can handle the AppDomain.UnhandledException event

EDIT: actually, this event is probably more adequate: Application.DispatcherUnhandledException

Thomas Levesque
Add the handler in the forms constructor like this: AppDomain.Current.UnhandledException+=...
Dabblernl
Bad idea if you create multiple instances of the window...
Thomas Levesque
Hi Thomas, thanks for your answer. Appdomain.UnHandledException works great for me.
Scott Olson
+5  A: 

You can trap unhandled exceptions at three levels:

  1. AppDomain.UnhandledException From all threads in the AppDomain.
  2. Dispatcher.UnhandledException From a single specific UI dispatcher thread.
  3. Application.DispatcherUnhandledException From the main UI dispatcher thread in your WPF application.

You should consider what level you need to trap unhandled exceptions at.

Deciding between the last two depends upon whether you're using more than one WPF thread. This is quite an exotic situation and if you're unsure whether you are or not, then it's most likely that you're not.

Drew Noakes
A: 

To supplement Thomas's answer, the Application class also has the DispatcherUnhandledException event that you can handle.

dustyburwell