Floating FB sharing byReview Results

Floating FB sharing byReview Results

  • WPF Tutorial

    • # Read in Your Language


    Dependency Property

    Tutorial Home | Dependency Properties | Attached Properties| Routed Events | Dispatcher in WPF | DispatcherUnhandledException

    DispatcherUnhandledException

    UnhandledException in Windows form:


    when an exception is not caught by an event handler within the application thread or within the current appdomain, then these exceptions can be caught in windows form application using the Application.ThreadException event handler and UnhandledExceptionEventHandler, and continue with the application without closing the application.

    Using Application.ThreadException event handler:

    
     Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);  
     Application.ThreadException += 
     new System.Threading.ThreadExceptionEventHandler(Application_Exception);
            
     //Handler
     static void Application_Exception(object sender, 
     System.Threading.ThreadExceptionEventArgs ex)
     {
        LogMessages.LogEx(ex.Exception);
        if (bValue)
        {
             ExceptionDialog eDialog = 
             new ExceptionDialog(ex.Exception);
             eDialog.ShowDialog();
        }
        return;
     }
            

    Using UnhandledExceptionEventHandler at appdomain level:

    
            AppDomain currentDomain = AppDomain.CurrentDomain;
            currentDomain.UnhandledException += 
            new UnhandledExceptionEventHandler(ExHandler);  
            static void ExHandler (object sender, 
            UnhandledExceptionEventArgs args)
             {
               Exception e = (Exception) args.ExceptionObject;
               Console.WriteLine("MyHandler caught : " + e.Message);
            }
            

    Using DispatcherUnhandledException in WPF:

    Similar event handling is possible in WPF using DispatcherUnhandledException where it catches unhandled exceptions and notifies user with dialog and shuts the application. If the application needs to continue then user should handle DispatcherUnhandledException .This event is raised by the application for the exceptions that are not handled in appdomain.

    DispatcherUnhandledExceptionEventArgs contains the information regarding exception and the dispatcher.

    Handled is set to true or false to continue processing.

    Sample code:

    The following sample shows how to process unhandled exceptions using the DispatcherUnhandledExceptionevent.

    
            using System.Windows; 
            using System.Windows.Threading;
    
            namespace DispatcherUnhandledSample
            {
                public partial class App : Application
                {
                    void App_DispatcherUnhandledException(object sender, 
                    DispatcherUnhandledExceptionEventArgs e)
                    {
                        // handle the exception
                        e.Handled = true;
                    }
                }
            }
            

    << Previous >> | << Next Topic>>