C++重新抛出异常给出错误

发布于 2024-09-25 14:43:38 字数 613 浏览 4 评论 0原文

我试图捕获“特定”异常(FormatException^ 或 OverflowException^),然后重新抛出它并在“一般”异常(Exception^)catch 块中捕获它。

运行时,我通过输入给它一个格式异常。然后我在对话框中收到此错误: “FutureValue.exe 中发生了类型为‘System.FormatException’的未处理异常 附加信息:输入字符串的格式不正确。” 当我单击“break”时,它会将我带到第 232 行。

以下是部分代码:

try
{
...
}
catch(FormatException^ ex)
{
      MessageBox::Show("FormatException Occured.  Message: " + ex->Message);
      throw;
}
***line# 232*** catch(OverflowException^ ex)
{
      MessageBox::Show("Overflow Occured. Message: " + ex->Message);
      throw;
}
catch(Exception^ ex)
{
      MessageBox::Show("TESTING");
}

I'm trying to catch a 'specific' exception (FormatException^ or OverflowException^) and then re throw it and catch it in the 'general' exception (Exception^) catch block.

When run, I give it a format exception through input. I then get this error in a dialog box:
"An unhandled exception of type 'System.FormatException' occurred in FutureValue.exe
Additional information: Input string was not in a correct format."
When I click 'break' it takes me to line # 232.

Here is the partial code:

try
{
...
}
catch(FormatException^ ex)
{
      MessageBox::Show("FormatException Occured.  Message: " + ex->Message);
      throw;
}
***line# 232*** catch(OverflowException^ ex)
{
      MessageBox::Show("Overflow Occured. Message: " + ex->Message);
      throw;
}
catch(Exception^ ex)
{
      MessageBox::Show("TESTING");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卷耳 2024-10-02 14:43:38

重新抛出表达式(不带赋值表达式的抛出)导致最初抛出的对象被重新抛出。因为异常已经在重新抛出表达式发生的范围内被捕获,所以它被重新抛出到下一个动态封闭的 try 块。因此,在重新抛出表达式发生的范围内,catch 块无法对其进行处理。

考虑到上述情况,您可能需要这样编写代码:

try
{
    try
    {
       //...
    }
    catch(FormatException^ ex)
    {
          MessageBox::Show("FormatException Occured.  Message: " + ex >Message);
          throw;
    }
    catch(OverflowException^ ex)
    {
          MessageBox::Show("Overflow Occured. Message: " + ex->Message);
          throw;
    }
}
catch(Exception^ ex)
{
      MessageBox::Show("TESTING");
}

The rethrow expression (throw without assignment_expression) causes the originally thrown object to be rethrown. Because the exception has already been caught at the scope in which the rethrow expression occurs, it is rethrown out to the next dynamically enclosing try block. Therefore, it cannot be handled by catch blocks at the scope in which the rethrow expression occurred.

Taking above into account, you may want to write your code like this:

try
{
    try
    {
       //...
    }
    catch(FormatException^ ex)
    {
          MessageBox::Show("FormatException Occured.  Message: " + ex >Message);
          throw;
    }
    catch(OverflowException^ ex)
    {
          MessageBox::Show("Overflow Occured. Message: " + ex->Message);
          throw;
    }
}
catch(Exception^ ex)
{
      MessageBox::Show("TESTING");
}
饭团 2024-10-02 14:43:38

上面有一个 try catch 块吗?

你只是用 throw 语句抛出了一个异常,没有人捕获它。
调试器已将您带到引发异常的位置

is there a try catch block above this?

You just threw an exception with the throw statement, nobody is catching it.
The debugger has taken you to where the exception was thrown

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文