Vb.net VB对未处理的异常,单击按钮并恢复循环?

Vb.net VB对未处理的异常,单击按钮并恢复循环?,vb.net,winforms,visual-studio,Vb.net,Winforms,Visual Studio,当我的程序弹出“未处理的异常”时,我可以在程序中调用按钮单击吗?我的表单中有一个按钮,它实际上可以“修复”或“绕过”异常。例如,某个“On Error Button2.PerformClick()3次”(只是我思考的一个示例)启用应用程序事件并从那里处理它,您可以使用未处理的异常函数来显示表单,请查看以下代码: Imports System.Text Imports System.IO Namespace My ' The following events are availabl

当我的程序弹出“未处理的异常”时,我可以在程序中调用按钮单击吗?我的表单中有一个按钮,它实际上可以“修复”或“绕过”异常。例如,某个“On Error Button2.PerformClick()3次”(只是我思考的一个示例)

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

 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

问题还不清楚。什么代码,什么按钮,什么循环?唯一可以给出的建议是将代码放入一个。文档和所有VB.NET教程都充分介绍了这一点。Google for
.NET异常处理
。我刚刚编辑了我的帖子,现在可能更清楚了。如果你想在出现异常时做些什么,你需要处理异常。使用Try/Catch块来执行此操作。或者,如果要绕过所有异常(不推荐),可以尝试订阅
应用程序。ThreadException
event:)@Shlomi一旦引发异常且未进行处理,则程序已崩溃。您可能有一个try/catch块,每当捕捉到异常时,该块都会通知用户并请求用户输入(例如,如您所说的按下按钮)