views:

806

answers:

3

Hi
Is there any way to handle all Errors Exceptions and crashes in WPF application? I know about DispatcherUnhandledException, but it handles only exceptions in UI thread, isn't it? Is there a way to catch and log all exceptions in other threads? and binding errors too? How do you implement this kind of requirement in your enterprice system?

+5  A: 
AppDomain.CurrentDomain.UnhandledException

Will catch any unhandled exceptions for the current thread. This is how we handle it in our application.

BindingErrors are always handled and logged to the output window. Before a release we check the output window for binding errors and fix as many as we can.

However it is my opinion that you would not want to treat binding errors as unhandled as they mostly recoverable and should be fixed as best you can before each release. You can change Debug > Exeptions in Visual Studio to make it throw BindingFailure to get more specific information.

HTH,

Dennis

Dennis Roche
Where is the best place to define the event handler such that even the earliest errors can be detected?
Ben McIntosh
You could register the event in application entry point.See http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx for the details as there a few things you need to understand.
Dennis Roche
+3  A: 

Keep in mind, that Microsoft does not recommend catching all exceptions, instead they recommend to catch only exceptions you know (or expect to happen in some place). Even more if you want to get "Certified for Microsoft [Windows|Vista]" logo, you must not catch unknown exceptions, and such exceptions must go to Wer.

arbiter
I agree, however if you do catch all exception do it only to log it, so you can find common errors and correct them.An then throw the exception again.
khebbie
I believe the Microsoft advice you mention is NOT to do catch all exceptions, i.e. catch Exception, from try block. However the OP is asking for a solution to handle uncaught exceptions so that you can handle the situation by informing the user of the crash and sending your development/QA team a crash report e-mail.
Dennis Roche
No, Microsoft advice is exactly about catching all unhandled exceptions. The main idea that os will collect all information and send report to MS, so MS will have all statistics about problem apps, and developer then can take this reports from MS though Winqual.But of course making some logging about unknown exception and then rethrow as khebbie mentioned is also allowed.
arbiter
Thank-you for the clarification. We do not submit error information to WER as our application is internal.
Dennis Roche
+1  A: 

Yes, there are 3 places:

  1. place Application.Run() into try ... catch
  2. DispatcherUnhandledException
  3. AppDomain.CurrentDomain.UnhandledException

In either case you should display a please-forgive-me message and suggest to send an error report.

The service on your server should answer either 'thank you for submitting error report' or 'the problem is already fixed in the next version. please update'

modosansreves