tags:

views:

42

answers:

1

We know that we can catch any unexpected exception at application level by using Application.Current.UnhandledException. Is there any way I can catch exceptions at page level rather than application level?

+1  A: 

Not really. A page doesn't really have any particular scope of execution. You could very well have multiple pages on a given screen at any time. But in the Silverlight navigation application project template in Visual Studio there's a really stupid block of code that hides exceptions from you and turns them into useless "navigation failed" messages.

This is in the Frame.NavigationFailed event which is raised for exceptions while navigating to a particular page. But not exceptions that occur afterwards. If you want to access the exception, it's in the EventArgs.

private void ContentFrame_NavigationFailed( object sender, NavigationFailedEventArgs e )
{

    e.Handled = true;

    // the navigation template does this. useless
    //ChildWindow errorWin = new ErrorWindow( e.Uri );

    // this will show the exception
    ChildWindow errorWin = new ErrorWindow( e.Exception );

    errorWin.Show( );
}
Josh Einstein