简体   繁体   English

为什么这个catch块没有捕获抛出的异常?

[英]Why is this catch block not catching the thrown exception?

I have this code: 我有这个代码:

private void btn1_Click(object sender, EventArgs e)
{
    try
    {
        Thrower();
    }
    catch
    {
        MessageBox.Show("exception caught");
    }
}

Which calls this method: 哪个调用此方法:

private void Thrower()
{
    throw new OverflowException();
}

Well I'm not very experienced when it comes to exception handling, but I would have thought that this message box ("exception caught") would show here. 嗯我在异常处理方面不是很有经验,但我会想到这个消息框(“异常捕获”)会显示在这里。 It doesn't. 它没有。 Should it be? 应该是吗? If not, what am I doing wrong? 如果没有,我做错了什么? Have I misunderstood how this is supposed to work? 我误解了这应该是怎么回事吗?

Thanks. 谢谢。

I tested this and it works fine for me. 我测试了这个,它对我来说很好。 Are you sure you sure the btn1_Click method is actually being called? 你确定你确定实际上正在调用btn1_Click方法吗? Maybe you forgot to wire up your events? 也许你忘记了你的活动?

在你的try / catch中放一个断点,看它是否进入那里(它不是)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM