Tuesday, November 11, 2008

How to Handle an Unhandled Exception in WPF?

By default Windows Presentation Foundation (WPF) catches unhandled exceptions and notifies the user. once user answer to the dialog box the application will shutsdown automatically.


Its also possible to handle this default behaviour and provide custom unhandled exception for that we need to override the DispatcherUnhandledException event. this way we can have centralized unhandled exception processing.



Namespace
System.Windows.Application.DispatcherUnhandledException Event
Occurs when an exception is thrown by an application but not handled by user.


DispatcherUnhandledException is raised by an Application for each exception that is unhandled by code running on the main UI thread. If an exception is not handled on either a background user interface (UI) thread (a thread with its own Dispatcher) or a background worker thread (athread without a Dispatcher), the exception is not forwarded to the main UI thread. Consequently, DispatcherUnhandledException is not raised.


In these circumstances, you will need to write code to do the following:


Handle exceptions on the background thread.


Dispatch those exceptions to the main UI thread.


Rethrow them on the main UI thread without handling them to allow DispatcherUnhandledException to be raised.






Once you handle the unhandled exception from DispatcherUnhandledException, you need to set the Handled property to true otherwise WPF will continue processing it,


The following example shows how to process unhandled exceptions by
handling the DispatcherUnhandledException event.





using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;

namespace ProjectExp {
///
/// Interaction logic for App.xaml
///

public partial class App : Application
{
private void App_Unhandle(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
string msg = string.Format("Expection occurs in {0}", e.Exception.Message);
MessageBox.Show(msg);
e.Handled = true;
}
}
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProjectExp {
///
/// Interaction logic for Window1.xaml
///

public partial class Window1 : Window {
public Window1() {
InitializeComponent();
}

private void btnUnhandle(object sender, RoutedEventArgs e)
{
throw new NotImplementedException("Failed"); // Here i am not handling the exception .
}

private void btnhandle(object sender, RoutedEventArgs e)
{
try
{
throw new NotImplementedException();
} catch (NotImplementedException ex) // Here Exception is handled
{

MessageBox.Show(ex.Message);
}
}
}
}



XAML CODE



Thanks
SreenivasaRagavan

No comments: