c++ - 异常处理(限制异常)

标签 c++ exception-handling

我正在处理以下代码,并感到困惑,请看一下

#include<iostream>
#include<conio.h>
#include<string.h>

using namespace std;

void Xhandler(int test) throw(char,double)  /*i am restricting an integer exception     here by not including it in the argument list of throw*/
{
if(test==0) throw test;
if(test==1) throw 'a';
if(test==2) throw 123.23;
}

int main()
{
cout<"start";

try{
    Xhandler(0);  
}

catch(int i)  /*this catch statement must be ignored then,but it is running*/
{
    cout<<"caught an integer"; /*this is the output on the screen*/
}
catch(char c)
{
    cout<<"caught character";
}
catch(double a)
{
    cout<<"caught double";
}

cout<<"end";
    _getch();
    return 0;

     }

int对应的catch语句必须被忽略(没有被忽略)&程序必须被终止,因为没有留下匹配的catch语句,但事实并非如此?

最佳答案

您没有指定编译器。如果您使用的是 Microsoft Visual C++,则在 Exception Specifications page 处有一条注释在 MSDN 上

Visual C++ departs from the ANSI Standard in its implementation of exception specifications. The following table summarizes the Visual C++ implementation of exception specifications:

...

throw(type) - The function can throw an exception of type type. However, in Visual C++ .NET, this is interpreted as throw(...).

当使用 g++ 时,进程在运行示例时由于意外异常而终止。

关于c++ - 异常处理(限制异常),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6891748/

相关文章:

c++ - := versus = error: undefined reference to qMain(int, 字符**)

c# - 捕获大多数派生异常?

Java 异常类

ruby-on-rails - 如何优雅地处理零个异常?

c++ - 为什么我应该在 unordered_map 参数前添加 '&'?

c++ - regex_search.hpp:56: 未定义对`boost::re_detail_106100::perl_matcher 的引用

c++ - 将前 n 个字符复制到 std::string

php - 将 PHP 错误发送到默认错误日志和用户定义的错误日志

python - 单行异常处理

c++ - 处理图像的多重转换