Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is best practice to handle all Exceptions in WPF application?

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, doesn't it?

Is there a way to catch and log all exceptions in other threads and binding errors too?

like image 207
Arsen Mkrtchyan Avatar asked Jun 24 '09 04:06

Arsen Mkrtchyan


People also ask

How do you handle exceptions in WPF?

Start by creating a new WPF project with the name WPFExceptionHandling. Drag one textbox from the toolbox to the design window. The following XAML code creates a textbox and initializes it with some properties. Here is the file reading with exception handling in C#.

What are the 3 approaches to handling exceptions in a web application?

try catch finally 2. Use error events to deal with exceptions within the scope of an object. Page_Error Global_Error Application_Error 3. Use custom error pages to display informational messages for unhandled exceptions within the scope of a Web application.


2 Answers

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.

like image 103
Dennis Avatar answered Oct 03 '22 17:10

Dennis


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'

like image 20
bohdan_trotsenko Avatar answered Oct 03 '22 16:10

bohdan_trotsenko