简体   繁体   中英

How to crash on unhandled Exception

I have created some custom exceptions in a WPF project and I am pretty sure that I am not catching them, when they get thrown, but my application does not crash either. The Exception is logged in the Output Window though.

My custom exceptions derive from System.Exception and I have checked all my try{}catch{} statements if they catch my custom Exception or System.Exception . None of them do that. I am using Visual Studio 2010 and am running it on a 32bit machine, so this seems to be a different problem.

You can add a handler for the AppDomain.UnhandledException event:

AppDomain.CurrentDomain.UnhandledException += App_UnhandledException;

public void App_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    Exception exception = (Exception)e.ExceptionObject;
    Error error = new Error(exception, mainViewModel.StateManager.CurrentUser);        
    mainViewModel.AddError(error); 
}

Error is a custom class that I use to log errors in the database with. If you put a break point in this method, then execution will jump here whenever there are any uncaught Exception objects that have been thrown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM