Tuesday, May 13, 2014

catch uncaught exceptions c# wpf

Hi,

today I heard about a solution to catch exceptions globally in C# with WPF:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            DispatcherUnhandledException += App_DispatcherUnhandledException;
        }

        void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            MessageBox.Show("error");
            e.Handled = true;
            this.Shutdown(1);
        }
    }

This snippet handles ALL exceptions which are not caught, shows the message "error" and quits with error.

Kind regards,
Daniel

No comments: