主题:关于异常处理的问题
请问下面的阻止抛出任何异常的程序应该没错吧,可不知道为什么运行后的结果是
catch an integer.也就是捕获了一个整型的异常吧。可是Xhandl()函数后面的throw()不是限制了抛出异常吗,为什么会捕获到异常呢?
请各位大侠指点迷津。先行谢过了!,
#include <iostream>
#include <exception>
using namespace std;
void Xhandl(int test)throw(/* char,double */)
{
if (test==0) throw test; //throw int
if (test==1) throw 'a'; //throw char
if (test==2) throw 123.456; //throw double
}
int main()
{
cout<<"start\n";
try
{
Xhandl(0);
}
catch(int x)
{
cout<<"caught an integer.\n";
}
catch (char x)
{
cout<<"caught a character.\n";
}
catch(double x)
{
cout<<"caught a double.\n";
}
cout<<"end\n";
return 0;
}
catch an integer.也就是捕获了一个整型的异常吧。可是Xhandl()函数后面的throw()不是限制了抛出异常吗,为什么会捕获到异常呢?
请各位大侠指点迷津。先行谢过了!,
#include <iostream>
#include <exception>
using namespace std;
void Xhandl(int test)throw(/* char,double */)
{
if (test==0) throw test; //throw int
if (test==1) throw 'a'; //throw char
if (test==2) throw 123.456; //throw double
}
int main()
{
cout<<"start\n";
try
{
Xhandl(0);
}
catch(int x)
{
cout<<"caught an integer.\n";
}
catch (char x)
{
cout<<"caught a character.\n";
}
catch(double x)
{
cout<<"caught a double.\n";
}
cout<<"end\n";
return 0;
}