Class ErrorHandler
Methods for handling unhandled exceptions of various kinds within a WPF application.
[Obsolete("This is deprecated and will be removed in a future version; Acuit.Pinpoint.ErrorHandling.IErrorHandler or Acuit.Windows.DefaultErrorHandler should be used instead.")]
public static class ErrorHandler
- Inheritance
-
ErrorHandler
- Inherited Members
Remarks
Handlers are added to handle unexpected errors from these sources:
- The application dispatcher.
- The current domain (to handle exceptions from non-UI threads).
- Unobserved task exceptions.
To handle all possible unexpected exceptions in a WPF application, do the following:
- In the application class (derived from Application), override OnStartup(StartupEventArgs) and call SetUpUnhandledExceptionHandling(Application, string) before doing anything else (including before calling base.OnStartup(StartupEventArgs)).
- If there is any program code that runs before the OnStartup(StartupEventArgs) call (e.g., in a custom Main method), then add a try/catch block that will catch any unhandled exceptions and call HandleUnexpectedException(Exception).
- If there are any other possible places where unexpected exceptions could occur without crashing triggering normal error handling (e.g., Prism navigation failure events, handled via Microsoft.Practices.Prism.Regions.IRegionNavigationService.NavigationFailed), call HandleUnexpectedException(Exception).
- Make sure that all async/await code follows best practices for Task-based asynchronous programming patterns, so that exceptions are properly handled. In particular, wrap any "fire-and-forget" tasks with AddFaultHandler(Task) to immediately handle unhandled exceptions (even though SetUpUnhandledExceptionHandling(Application, string) will set up a handler for unobserved task exceptions which will eventually be triggered by the task finalizer). In other words, watch out for any calls to asynchronous methods that do nothing with the returned Task object.
Properties
AsIErrorHandler
Gets or sets an IErrorHandler to use to handler unexpected exceptions, for use by legacy code that does not have an IErrorHandler available.
public static IErrorHandler AsIErrorHandler { get; set; }
Property Value
Remarks
By default, this simply defers to the legacy HandleUnexpectedException(Exception) method, but it can be set during application startup to use the same unexpected error handler as the rest of the application (i.e., so unexpected error log messages will all use the same logger name and formatting).
Methods
AddFaultHandler(Task)
Add a fault handler to an asynchrounous task to handle unhandled exceptions.
public static Task AddFaultHandler(Task task)
Parameters
task
TaskThe task for which to add the fault handler.
Returns
- Task
A new continuation Task.
Remarks
This should be used when any "fire-and-forget" tasks are started.
Exceptions
- ArgumentNullException
task
is null.
HandleUnexpectedException(Exception)
Handle an unexpected exception by logging, displaying an error message to the user, and immediately closing the application.
public static void HandleUnexpectedException(Exception exception)
Parameters
exception
ExceptionThe unexpected exception.
Remarks
Because this will trigger a message box, care must be taken about what thread this is called from. It is best to call from the main UI thread. It should definitely not be called from the object finalizer thread.
SetUpUnhandledExceptionHandling(Application, string)
Sets up handlers for various sources of unhandled application exceptions.
public static void SetUpUnhandledExceptionHandling(Application application, string applicationName)
Parameters
application
ApplicationThe application for which to set up exception handlers.
applicationName
stringThe application name to use in error messages to the user.
Remarks
This should be called in the application's Application-derived class's OnStartup override, before performing any other application initialization. Note that handlers are not set up when running in debug mode with a debugger attached, as it is assumed that when an unhandled exception occurs, the debugger will break into the program at the point of the exception, allowing it to be debugged before closing down the program.
Exceptions
- ArgumentNullException
application
is null.- ArgumentNullException
applicationName
is null.