简体   繁体   English

Windows:主线程中未处理的异常

[英]Windows: exceptions not handled in main thread

Is it possible in windows to ignore exceptions not handled in main thread? 在Windows中是否可以忽略未在主线程中处理的异常?

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? 但是,为什么在Windows中为System.Threading.Timer任务生成的线程中未处理的异常会在Windows中产生此“您要打开调试器”对话框吗?

Yes, you can always wrap the whole task in a try/catch-all block, but I find that odd. 是的,您始终可以将整个任务包装在try / catch-all块中,但是我觉得很奇怪。

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. 注意:我尝试了一个简单的命令行程序,该程序在Timer对象中有一个引发异常的任务(几秒钟后)-似乎仍然无法阻止Windows终止进程。

If your tread can continue working even after the exception then a global try catch wrap would be enough. 如果您的胎面即使在异常发生后仍能继续工作,则进行全局try catch wrap就足够了。 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. 如果您不知道异常是如何发生的,并且不想将漏洞代码包装到try catch中,则可以使用UnhandledException处理程序记录该错误,以便将来进行更正。

Here is an example in vb.net: 这是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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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