Catching application crash events

为君一笑 提交于 2019-11-30 17:52:57

问题


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:


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.



来源:https://stackoverflow.com/questions/10519161/catching-application-crash-events

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