【问题标题】:VB on unhandled exception, Click button and resume loop?VB关于未处理的异常,单击按钮并恢复循环?
【发布时间】:2017-08-30 13:51:31
【问题描述】:

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

【问题讨论】:

  • 问题不清楚。什么代码,什么按钮,什么循环?唯一可以给出的建议是将代码放在try/catch block 中。这在文档和所有 VB.NET 教程中都有充分的介绍。谷歌.NET Exception Handling
  • 我刚刚编辑了我的帖子,现在可能更清楚了..
  • 如果你想在有异常的时候做点什么,你需要处理异常。使用 Try/Catch 块来执行此操作。
  • 或者,如果您想绕过所有异常(不推荐),您可以尝试订阅Application.ThreadException 事件:)
  • @Shlomi 一旦抛出异常并且不处理,程序已经崩溃。你可能有一个 try/catch 块,每当捕获到异常时通知用户并要求他输入(例如,就像你说的按下按钮)

标签: vb.net winforms visual-studio


【解决方案1】:

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

 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

【讨论】:

    猜你喜欢
    • 2016-12-27
    • 2014-06-24
    • 2020-09-19
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多