【发布时间】:2023-03-27 21:33:01
【问题描述】:
在桌面应用程序中我设置了琐碎
如果有人想要codez(那里没什么有趣的):
Namespace My
Partial Friend Class MyApplication
' Thread exception handler. Handles all unhandled exceptions on UI thread.
Private Sub UIThreadException(sender As Object, e As ThreadExceptionEventArgs)
CustomErrorHandler.Handle(e.Exception, "[UIThreadException]")
End Sub
' Handle the UI exceptions by showing a dialog box, and asking the user whether
' or not they wish to abort execution.
' NOTE: This exception cannot be kept from terminating the application - it can only
' log the event, and inform the user about it.
Private Sub CurrentDomain_UnhandledException(sender As Object,
e As UnhandledExceptionEventArgs)
Try
Dim ex As Exception = CType(e.ExceptionObject, Exception)
CustomErrorHandler.OnUnhandled(ex)
Catch exc As Exception
MsgBox(exc.ToString(), vbCritical, "Last resort error printout")
End Try
End Sub
End Class
End Namespace
有时,当我从Form1 的ButtonOK_Click(...) 事件处理程序中抛出异常时会有所不同:
- 当作为独立启动 (
app.exe) 时,UIThreadException()会按预期调用 - 当从 Visual Studio (
app.vshost.exe) 内部启动时,CurrentDomain_UnhandledException()被调用在同一场景中(代码中与之前相同的位置,相同的数据),忽略UIThreadException()处理程序
所以在独立的 exe 中,所有的工作都可靠,但在调试会话期间,在相同的情况下陷入未处理的异常是非常烦人的。
我可以在 Visual Studio 2012 和 2015 中观察到相同的行为。如果在简单的示例上进行测试,它在调试会话中也总是能按预期工作。如果代码更复杂(嵌套调用中引发的异常等),则 UI 线程异常处理程序将被忽略。
你们中是否有人能够观察到同样的问题并找到解决方案?
【问题讨论】:
标签: vb.net visual-studio-2012 exception-handling visual-studio-2015 .net-3.5