Catching application crash events

2019-02-14 07:02发布

I made a application in VB.Net. But some users face crash upon startup. That is "A problem caused this program from working correctly" with just one button "Close the program". Since there are lot of things happening when the app loads, is it possible to know what caused the issue?

1条回答
forever°为你锁心
2楼-- · 2019-02-14 07:23

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.

查看更多
登录 后发表回答