gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:06:45 25 4
gpt4 key购买 nike

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

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com