Catching application crash events

喜夏-厌秋 提交于 2019-11-30 23:00:39

If the "Application Framework" is enabled in your project's properties, click the "View Application Events" button on the "Application" project properties page. Then add an event handler:

Partial Friend Class MyApplication
    Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
        ' ...
    End Sub
End Class

If you are not using the application framework, you should put a try catch block around your entire Main method. However, that will only catch exceptions that occur on the primary thread. If your application is multi-threaded, you can handle exceptions from all threads by creating a method like this:

Public Sub UnhandledExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
    ' ...
End Sub

And then attach it to your current domain's UnhandledException event, like this:

AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionHandler

That event handler will then get called for all unhandled exceptions from anywhere in your domain, regardless of the current thread.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!