捕捉未处理的异常决定
Catching unhandled exceptions decision
我决定在我的一个应用程序上启用严格选项。对于我的一生,我无法弄清楚如何编译一小段代码。在一个模块中我有这段代码
Sub Main()
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException
End Sub
Private Sub CurrentDomain_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs)
e.ExitApplication = False
End Sub
环顾四周并看到另一个 post 关于将其放入 ApplicationEvents 后,我通过处理上述事件使其工作。所以出于好奇,我决定将 AddHandler 移动到同一个 class 中,然后很明显它的 class 名称相同但名称空间不同:
Partial Friend Class MyApplication
Private Sub MyApplication_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
e.ExitApplication = False
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException
End Sub
Private Sub CurrentDomain_UnhandledException(sender As Object, e As System.UnhandledExceptionEventArgs)
e.ExitApplication = False
End Sub
End Class
这是对两者的 link:
- Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs
- System.UnhandledExceptionEventArgs
所以我的问题是,我应该使用哪一个?我想阻止应用程序关闭...但这两个选项似乎都是我想要的。
System.UnhandledException
没有 ExitApplication
成员(请参阅您链接的文档),因此不能用于停止应用程序退出 - 一旦调用此应用程序将始终终止。通常,Microsoft.VisualBasic
命名空间是 VB 的助手,它们或多或少地重复了其他地方可用的功能。与您提到的 VisualBasic 处理程序最接近的等效项是 Application.ThreadException
一个。这和 AppDomain.CurrentDomain.UnhandledException
在 MS docs.
中都有很好的描述
要防止应用程序关闭,可以使用 VisualBasic
或 ThreadException
之一。我过去曾使用 Microsoft.VisualBasic
来实现与您正在做的类似的事情。
我决定在我的一个应用程序上启用严格选项。对于我的一生,我无法弄清楚如何编译一小段代码。在一个模块中我有这段代码
Sub Main()
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException
End Sub
Private Sub CurrentDomain_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs)
e.ExitApplication = False
End Sub
环顾四周并看到另一个 post 关于将其放入 ApplicationEvents 后,我通过处理上述事件使其工作。所以出于好奇,我决定将 AddHandler 移动到同一个 class 中,然后很明显它的 class 名称相同但名称空间不同:
Partial Friend Class MyApplication
Private Sub MyApplication_UnhandledException(sender As Object, e As UnhandledExceptionEventArgs) Handles Me.UnhandledException
e.ExitApplication = False
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException
End Sub
Private Sub CurrentDomain_UnhandledException(sender As Object, e As System.UnhandledExceptionEventArgs)
e.ExitApplication = False
End Sub
End Class
这是对两者的 link:
- Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs
- System.UnhandledExceptionEventArgs
所以我的问题是,我应该使用哪一个?我想阻止应用程序关闭...但这两个选项似乎都是我想要的。
System.UnhandledException
没有 ExitApplication
成员(请参阅您链接的文档),因此不能用于停止应用程序退出 - 一旦调用此应用程序将始终终止。通常,Microsoft.VisualBasic
命名空间是 VB 的助手,它们或多或少地重复了其他地方可用的功能。与您提到的 VisualBasic 处理程序最接近的等效项是 Application.ThreadException
一个。这和 AppDomain.CurrentDomain.UnhandledException
在 MS docs.
要防止应用程序关闭,可以使用 VisualBasic
或 ThreadException
之一。我过去曾使用 Microsoft.VisualBasic
来实现与您正在做的类似的事情。