Unhandled exception

前端 未结 1 673

What is the best way to handle an unhandled exception in a WPF application?

1条回答
  •  旧巷少年郎
    2021-01-11 18:42

    You can use DispatcherUnhandledException:

    XAML (App.xaml):

    
    

    Code Behind (App.xaml.cs/vb:

    private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        // Handle error here
    
        ...
    
        // Prevent default unhandled exception processing by WPF
        e.Handled = true;
    }
    

    Read up more here. Always do the correct amount of error handling in the first place though. Don't just let errors slip into this method.

    0 讨论(0)
提交回复
热议问题