Search code examples
c#wpfunhandled-exception

Unhandled exception occurs after handling exception


This theme from my preveus question: How to remove and create log in Windows Event Viewer

I created wpf app. I'm catching Unhandled exception with 3 ways:

public partial class App : Application
{
    public App()
    {
        DispatcherUnhandledException += App_DispatcherUnhandledException;
        Dispatcher.UnhandledException += Dispatcher_UnhandledException;
        AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
    }

    private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {

    }

    private void Dispatcher_UnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {

    }

    private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {

    }
}

I'm creating exception like this:

   public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        throw new Exception("Test exception");
    }
}

After executing method (Dispatcher_UnhandledException, CurrentDomain_UnhandledException,App_DispatcherUnhandledException) this exception is still throwing. And Windows Event Viewer is creating log like this

Description: The process was terminated due to an unhandled exception. Exception Info: System.InvalidOperationException at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(System.Data.Common.DbConnection, System.Threading.Tasks.TaskCompletionSource1<System.Data.ProviderBase.DbConnectionInternal>, System.Data.Common.DbConnectionOptions, System.Data.ProviderBase.DbConnectionInternal, System.Data.ProviderBase.DbConnectionInternal ByRef) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(System.Data.Common.DbConnection, System.Data.ProviderBase.DbConnectionFactory, System.Threading.Tasks.TaskCompletionSource1


Solution

  • In your handler method, you need to tell .NET that you handled the exception. Otherwise it will still kill the application. You do this by using the Handled property of the DispatcherUnhandledExceptionEventArgs object.

    So if you decide that you want to continue the application despite the exception, set it to true:

    private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
    {
        e.Handled = true; //let's continue and see if anything explodes
    }
    

    That said, you should still handle exceptions where they could occur (as much as possible). Take the example you gave, which looks like a network connection problem. If you caught that exception where you create the connection, you can tell the user something more specific like "I could not connect to the database. Is your network working?" Whereas if you rely on this to catch that kind of error, you can only regurgitate the exception message, which will often not make sense to the user.

    This should be used as a last-resort fail-safe. It should not replace catching exceptions throughout your code.