VB上未处理的异常,点击按钮并恢复循环?

时间:2017-08-30 13:51:32

标签: vb.net winforms visual-studio

当我的程序弹出' UnHandled Exception'时,我可以在程序中调用按钮点击吗?我的表单中有一个按钮,可以实际修复'或者'四处走动例外。喜欢什么东西' On Error Button2.PerformClick()3次' (只是我思考的一个例子)

1 个答案:

答案 0 :(得分:0)

启用应用程序事件并从那里处理它,您可以使用未处理的异常函数来显示您的表单,请看下面的代码:

 Imports System.Text
Imports System.IO

Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication



        'One of the global exceptions we are catching is not thread safe, 
        'so we need to make it thread safe first.
        Private Delegate Sub SafeApplicationThreadException(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)


        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup

            'There are three places to catch all global unhandled exceptions:
            'AppDomain.CurrentDomain.UnhandledException event.
            'System.Windows.Forms.Application.ThreadException event.
            'MyApplication.UnhandledException event.
            AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf AppDomain_UnhandledException
            AddHandler System.Windows.Forms.Application.ThreadException, AddressOf app_ThreadException
            ' AddHandler AccessViolationException, AddressOf app_AccessViolationException


        End Sub


        'Private Sub app_AccessViolationException(ByVal sender As Object, ByVal ex As System.AccessViolationException)

        'End Sub

        Private Sub app_ThreadException(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)

            'This is not thread safe, so make it thread safe.
            If MainForm.InvokeRequired Then
                ' Invoke back to the main thread
                MainForm.Invoke(New SafeApplicationThreadException(AddressOf app_ThreadException), _
                    New Object() {sender, e})
            Else
                frmDebug.Show()
            End If

        End Sub

        Private Sub AppDomain_UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)

            frmDebug.Show()

        End Sub


        Private Sub MyApplication_UnhandledException(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs)   Handles Me.UnhandledException

            frmDebug.Show()

        End Sub



    End Class


End Namespace