首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FormClosing和未处理异常

FormClosing和未处理异常
EN

Stack Overflow用户
提问于 2011-10-19 20:21:38
回答 1查看 1.1K关注 0票数 2

我从一个具有FormClosing处理程序的表单继承我的类(它是可重写的,所以在调用基方法之前,我可以重写它并在那里执行MsgBox("Ha") )。在我的共享Sub New()中,我有:

代码语言:javascript
复制
' Add the event handler for handling UI thread exceptions to the event.
AddHandler Application.ThreadException, AddressOf Application_ThreadException

' Add the event handler for handling non-UI thread exceptions to the event. 
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException

'For Console applications you should use the System.AppDomain.UnhandledException event
AddHandler Thread.GetDomain().UnhandledException, AddressOf CurrentDomain_UnhandledException

如果在try/catch之外以我的形式抛出一个异常,我会注意到两种不同的行为:

  1. ,如果我使用VS调试器,就会得到一个未处理的异常,并且似乎调用了未处理的异常处理程序。formClosing事件处理程序似乎没有被调用。
  2. ,如果我不使用VS调试器,则调用FormClosing处理程序,但不调用未处理的异常处理程序.

我很抱歉,为什么没有在#2中调用未处理的异常处理程序,理想情况下,我希望(在这两种情况下)调用未处理的异常,然后调用FormClosing事件处理程序。我遗漏了什么?

(一些示例代码) --这演示了如果在VS中使用调试器运行(只是按F5)或在没有调试器的情况下运行(只需按CTRL-F5),则会引发不同的异常。这并不能很好地再现问题,但也许我的问题与基类以不同方式处理ThreadException有关。

Form1 (在您的项目中设置为启动)。

代码语言:javascript
复制
Imports System.Threading

Public Class Form1
    Inherits Form2

    Shared Sub New()
        ' Add the event handler for handling UI thread exceptions to the event. 
        AddHandler Application.ThreadException, AddressOf Application_ThreadException

        ' Add the event handler for handling non-UI thread exceptions to the event.  
        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf CurrentDomain_UnhandledException

    End Sub

    Private Shared Sub CurrentDomain_UnhandledException(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
        MsgBox("UnhandledException caught")
    End Sub

    Private Shared Sub Application_ThreadException(ByVal sender As Object, ByVal e As ThreadExceptionEventArgs)
        MsgBox("ThreadException caught")
    End Sub

    Protected Overrides Sub Login()
        Throw New Exception("Ha!")

        MyBase.Login()
    End Sub

    Protected Overrides Sub Form2_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs)
        MsgBox("Form1Closing")

        MyBase.Form2_FormClosing(sender, e)
    End Sub
End Class

Form2:

代码语言:javascript
复制
Public Class Form2

  Protected Overridable Sub Form2_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        MsgBox("Form2Closing")
    End Sub

    Private Sub Form2_Shown(sender As System.Object, e As System.EventArgs) Handles MyBase.Shown
        Login()
    End Sub

    Protected Overridable Sub Login()
        MsgBox("Form2 LoggingIn")
    End Sub
End Class
EN

回答 1

Stack Overflow用户

发布于 2012-01-05 19:31:40

我很抱歉没有使用评论部分,也许它应该在那里(没有足够的代表)。我只是想把我的2c研究投入到:

settings.

  • Ported definition.

  • With调试器使用VS2010 SP1.

  • 创建了带有默认的WinApp项目,在Form2类调试器中添加了"Inherits“语句-获取"UnhandledException捕捉”消息,然后调试器在抛出新异常时中断(“Ha!”)行,继续使我陷入一个无限循环(再次获得"UnhandledException捕捉“消息,等等)。没有调试器的
  • -获取"UnhandledException捕捉”,并且在结束表单时继续执行,得到"Form1Closing“和Form1Closing。

一切似乎都如期而至。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7827632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档