Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unhandled exception

What is the best way to handle an unhandled exception in a WPF application?

like image 582
Maurizio Reginelli Avatar asked Feb 12 '10 12:02

Maurizio Reginelli


People also ask

What is an unhandled exception?

An unhandled exception is an error in a computer program or application when the code has no appropriate handling exceptions.

Which event is used for unhandled exceptions?

The UnhandledException event is raised for unhandled exceptions thrown in other threads.


1 Answers

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.

like image 189
Kyle Rosendo Avatar answered Sep 22 '22 23:09

Kyle Rosendo