Thursday 14 March 2013

Posted by Prasad KM | 21:16 Categories:

Exception Handling in WPF

 

By default, Windows Presentation Foundation (WPF) catches unhandled exceptions, notifies users of the exception from a dialog box (from which they can report the exception), and automatically shuts down an application.
However, if an application needs to perform custom unhandled exception processing from a centralized location, you should handle DispatcherUnhandledException.
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 (a thread 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:
  1. Handle exceptions on the background thread.
  2. Dispatch those exceptions to the main UI thread.
  3. Rethrow them on the main UI thread without handling them to allow DispatcherUnhandledException to be raised.
For more information, see the Threading Model overview.
The DispatcherUnhandledException event handler is passed a DispatcherUnhandledExceptionEventArgs argument that contains contextual information regarding the exception, including:
You can use this information to determine whether an exception is recoverable or not. A recoverable exception might be a FileNotFoundException, for example, while an unrecoverable exception might be a StackOverflowException, for example.
When you process an unhandled exception from DispatcherUnhandledException, and you don't want WPF to continue processing it, you need to set the Handled property to true.
Unlike the other events that Application raises, DispatcherUnhandledException does not have a matching protected virtual implementation (OnDispatcherUnhandledException). Consequently, classes that derive from Application must always register an event handler with DispatcherUnhandledException to process unhandled exceptions.


using System.Windows; // Application 
using System.Windows.Threading; // DispatcherUnhandledExceptionEventArgs 

namespace SDKSample
{
    public partial class App : Application
    {
        void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            // Process unhandled exception


...


            // Prevent default unhandled exception processing
            e.Handled = true;
        }
    }
}

In XAML, you can hook an application-level event that will fire whenever an unhandled error occurs anywhere in your application by overriding the OnStartup event in the App.xaml.cs file.
Here's an example excerpt:

protected override void OnStartup(StartupEventArgs e)
{
   // define application exception handler
   Application.Current.DispatcherUnhandledException  += new 
      DispatcherUnhandledExceptionEventHandler(
         AppDispatcherUnhandledException);

   // defer other startup processing to base class
   base.OnStartup(e);
}    
        
void AppDispatcherUnhandledException(object sender, 
   DispatcherUnhandledExceptionEventArgs e)
{
   //do whatever you need to do with the exception
   //e.Exception

   e.Handled = true;
}
 
Now the AppDispatcherUnhandledException event will fire whenever your code doesn't handle the exception at a lower level. The exception itself is in the DsipatcherUnhandledExceptionEventArgs object (e.Exception).

 

0 comments:

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube