简体   繁体   中英

Windows: exceptions not handled in main thread

Is it possible in windows to ignore exceptions not handled in main thread?

I understand that the OS would like to stop a process with an unhandled exception in the main thread. But why unhandled exceptions in a thread spawn for a System.Threading.Timer task produce this "do you want to open debugger" dialog in Windows?

Yes, you can always wrap the whole task in a try/catch-all block, but I find that odd.

Note: I tried a simple command line program, that has a task in a Timer object that throws an exception (after few seconds) - still it seems there is no way of stopping Windows from killing the process.

If your tread can continue working even after the exception then a global try catch wrap would be enough. The question is, is your thread able to continue after the exception? How vital is that exception for your thread? And if it is vital don't you want to know that happened?

if you do not know how the exception occurred and you do not want to wrap the hole code into a try catch you can use an UnhandledException handler to log the error in order to correct it in the future.

Here is an example in vb.net:

Dim currentDomain As AppDomain = AppDomain.CurrentDomain
AddHandler currentDomain.UnhandledException, AddressOf MyHandler


Sub MyHandler(ByVal sender As Object, ByVal args As UnhandledExceptionEventArgs)
    Dim e As Exception = DirectCast(args.ExceptionObject, Exception)
End Sub 'MyUnhandledExceptionEventHandler

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