Handle global exceptions in VB

流过昼夜 提交于 2019-12-06 01:05:13

问题


Hello I have this project experiencing some problems with what is supposed to be my codes for the "problem" handler.

Public Event UnhandledException As UnhandledExceptionEventHandler

 Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim currentDomain As AppDomain = AppDomain.CurrentDomain

            AddHandler currentDomain.UnhandledException, AddressOf MyHandler
        End Sub

    Sub MyHandler(ByVal sender As Object, ByVal args As UnhandledExceptionEventArgs)
            Dim e As Exception = DirectCast(args.ExceptionObject, Exception)

            Using sw As New StreamWriter(File.Open(myFilePath, FileMode.Append))
                sw.WriteLine(Date.now & e.toString)
            End Using

            MessageBox.Show("An unexcpected error occured. Application will be terminated.")
            Application.Exit()
        End Sub

        Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
            Throw New Exception("Dummy Error")
        End Sub

I'm trying to globally catch all exceptions and create logfile during runtime, which works fine in the debugger(exception handling and textfile writing) but cannot catch any unhandled exceptions after I build it in the setup project and Installed into a machine. What am I missing? Do I need to include additional components to my setup project? Help would be greatly appreciated


回答1:


There is already a way to handle exceptions for the entire application. Embedding the handler in a form means they would only be caught and logged if and while that form was open.

  1. Go to Project -> Properties -> Application and click the "View Application Events" button at/near the bottom.

  2. This will open ApplicationEvents.vb.

  3. Select (MyApplicationEvents) in the left menu; and UnhandledException in the right. This opens an otherwise typical event handler to which you can add code:

    Private Sub MyApplication_UnhandledException(sender As Object,
                                                 e As ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
    
        Dim myFilePath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                                                "badjuju.log")
    
        Using sw As New StreamWriter(File.Open(myFilePath, FileMode.Append))
            sw.WriteLine(DateTime.Now)
            sw.WriteLine(e.Exception.Message)
        End Using
    
        MessageBox.Show("An unexcpected error occured. Application will be terminated.")
        End
    
    End Sub
    

This will not catch exceptions while the IDE is running because VS catches them first so you can see them and fix them.



来源:https://stackoverflow.com/questions/30488132/handle-global-exceptions-in-vb

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