Printer Friendly Version      Send     
Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight
Application Class
Application Events
 UnhandledException Event
.NET Framework Class Library for Silverlight
Application..::.UnhandledException Event
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Occurs when an exception that is raised by Silverlight is not handled.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)
XMLNS for XAML: Not mapped to an xmlns.

Visual Basic (Declaration)
Public Event UnhandledException As EventHandler(Of ApplicationUnhandledExceptionEventArgs)
Visual Basic (Usage)
Dim instance As Application
Dim handler As EventHandler(Of ApplicationUnhandledExceptionEventArgs)

AddHandler instance.UnhandledException, handler
C#
public event EventHandler<ApplicationUnhandledExceptionEventArgs> UnhandledException
Visual C++
public:
 event EventHandler<ApplicationUnhandledExceptionEventArgs^>^ UnhandledException {
    void add (EventHandler<ApplicationUnhandledExceptionEventArgs^>^ value);
    void remove (EventHandler<ApplicationUnhandledExceptionEventArgs^>^ value);
}
JScript
JScript does not support events.
XAML Attribute Usage
<Application UnhandledException="eventhandler"/>

By default, Silverlight will detect and process unhandled exceptions that are raised from any part of the Silverlight application. If you would prefer to perform unhandled exception detection and processing, you can handle the UnhandledException event, which is raised by Application for each thrown exception that is unhandled by application code.

The UnhandledException event handler is passed a ApplicationUnhandledExceptionEventArgs object, which stores a reference to the unhandled exception in its ExceptionObject property. You can use this information to determine whether an exception is recoverable or not.

When you detect and process an unhandled exception and do not want Silverlight to continue processing it, you need to set the Handled property to true. If you leave the Handled property as false when exiting the handler, then the unmanaged exception handling as registered for the onError event takes place for the unhandled exception. An onError handler can also be handled by managed code, if you marshal the handler.

The following example shows how to handle and use the UnhandledException event.

XAML
<Application 
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Class="SilverlightApplication.App"
    Startup="App_Startup"
    UnhandledException="App_UnhandledException">

</Application>

C#
using System.IO; // FileNotFoundException
using System.Windows; // Application, StartupEventArgs, ApplicationUnhandledExceptionEventArgs

namespace SilverlightApplication
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
        }

        private void App_Startup(object sender, StartupEventArgs e)
        {
            // Specify the root visual
            this.RootVisual = new Page();
        }

        private void App_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject is FileNotFoundException)
            {
                // Inform the user

                // Recover from the error
                e.Handled = true;
                return;
            }

            // Exception is unrecoverable so stop the application and allow the 
            // Silverlight plug-in control to detect and process the exception.
        }
    }
}

Run this sample.

© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker