What is the best way to handle an unhandled exception in a WPF application?
A:
try/catch/finally block around the code and handle the exception with error handling.
Scott Vercuski
2010-02-12 12:54:22
This is a very broad statement. You can't catch everything in WPF sometimes. hence the need for the DispatcherUnhandledException.
daub815
2010-02-16 15:09:40
+3
A:
You can use DispatcherUnhandledException
:
XAML (App.xaml):
<Application x:Class="App.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="wndMain.xaml" DispatcherUnhandledException="Application_DispatcherUnhandledException">
Code Behind (App.xaml.cs/vb:
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
// Handle error here
...
// Prevent default unhandled exception processing by WPF
e.Handled = true;
}
Read up more here. Always do the correct amount of error handling in the first place though. Don't just let errors slip into this method.
Kyle Rozendo
2010-02-12 12:55:41