WPFGlobalExceptionHandling 1.0.0
dotnet add package WPFGlobalExceptionHandling --version 1.0.0
NuGet\Install-Package WPFGlobalExceptionHandling -Version 1.0.0
<PackageReference Include="WPFGlobalExceptionHandling" Version="1.0.0" />
<PackageVersion Include="WPFGlobalExceptionHandling" Version="1.0.0" />
<PackageReference Include="WPFGlobalExceptionHandling" />
paket add WPFGlobalExceptionHandling --version 1.0.0
#r "nuget: WPFGlobalExceptionHandling, 1.0.0"
#addin nuget:?package=WPFGlobalExceptionHandling&version=1.0.0
#tool nuget:?package=WPFGlobalExceptionHandling&version=1.0.0
Windows Presentation Foundation (WPF) Global Exception Handling
For handling exceptions in the App.xaml.cs
file of any .NET Framework WPF Application.
About
This project was created with a desire to handle all exceptions (or unhandled exceptions if that's you're desire) at a global level in WPF applications
Usage
Add WPFGlobalExceptionHandling to your list of references
Add
using WPFGlobalExceptionHandling;
to the header of yourApp.xaml.cs
fileAdd the interface
IWPFGlobalExceptionHandler
to theApp.xaml.cs
class- Example:
public partial class App : Application, IWPFGlobalExceptionHandler { }
- Example:
Implement the two interface member methods that
IWPFGlobalExceptionHandler
interfacesHandleException()
handles "normal" unhandled exeptions that can be safely handled- Example:
public void HandleException(Exception e) { // logs the exception, including inner exceptions logger.Error(e); // shows a message box with the root exception MessageBox.Show(e.GetRootException().Message, "Uh-oh, something went wrong!", MessageBoxButton.OK, MessageBoxImage.Error); }
HandleUnrecoverableException()
handles "unrecoverable" unhandled exeptions and then closes the application- Example:
public void HandleUnrecoverableException(Exception e) { // logs the exception, including inner exceptions logger.Fatal(e); // shows a message box with the root exception MessageBox.Show($"The application is now going to close.\n\n'{e.GetRootException().Message}'", "Unrecoverable Error!", MessageBoxButton.OK, MessageBoxImage.Error); } ```
In the
App.xaml.cs
constructor, add a call tothis.UseGlobalExceptionHandling();
- This uses the extension method in WPFGlobalExceptionHandling for Application classes interfacing IWPFGlobalExceptionHandler
- Four different handlers are used to handle exceptions globally:
AppDomain.CurrentDomain.UnhandledException
handles non-UI thread exceptions thrown; the app terminates after unhandled exceptions are caught hereapp.DispatcherUnhandledException
handles UI dispatcher thread exceptions thrownTaskScheduler.UnobservedTaskException
handles domain-wide exceptions where a task scheduler is used for asynchronous operationsDataBindingErrorHandler.ThrowExceptionsWithDataBindingErrors()
allows DataBinding errors to throw exceptions, which are then caught by DispatcherUnhandledException
Now any time an exception occurs at any level, on any thread, it will be caught at
App
level and handled in one of the two interface methods, HandleException() or HandleUnrecoverableException()
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net452 is compatible. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 1,254 | 6/25/2018 |