【问题标题】:Exceptions thrown in the `OnLoad` method are ignored? [duplicate]`OnLoad` 方法中抛出的异常被忽略? [复制]
【发布时间】:2013-10-02 17:01:45
【问题描述】:

我有这段代码

partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        throw new InvalidOperationException();
        base.OnLoad(e);
    }
}

程序运行良好,我在OnLoad(EventArgs e) 中抛出的异常被完全忽略。我已经在 VS2012 .NET 4.5 的调试和发布模式下对其进行了测试,结果相同。这很正常,如果是,为什么?

【问题讨论】:

  • @HansPassant 非常感谢我正在寻找的东西。

标签: c# winforms .net-4.5


【解决方案1】:

这是因为您的 VS 中的 Exceptions option。如果您查看Output 窗口,您应该会看到这样打印的消息A first chance exception of type 'System.InvalidOperationException' occurred in...

要启用抛出异常,您可以转到Debug -> Exceptions -> Common Language Runtime Exceptions,然后您可以展开命名空间并找到您想要的异常,在您的情况下是@987654324 @。然后检查Thrown列中的Thrown。

【讨论】:

  • 你是对的这项工作,但不明白为什么只在 OnLoad 中忽略异常,而不是无处不在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-29
  • 2013-03-20
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
  • 2018-12-12
相关资源
最近更新 更多