1、问题1
2、
3、
4、问题2、
5、
#include <iostream>
using namespace std;
void func() throw(int)
{
cout << "func()";
cout << endl;
throw 'c';
}
int main()
{
try
{
func();
}
catch(int)
{
cout << "catch(int)";
cout << endl;
}
catch(char)
{
cout << "catch(char)";
cout << endl;
}
return 0;
}
不同的编译器结果会不一样:
G++编译器和BCC编译器结果一样:
func()
terminate called after throwing an instance of 'char'
Aborted (core dumped)
异常终止
VC编译器:
func()
catch(char)
为什么会出现上述结果,原因在如下图:
6、
7、
#include <iostream>
#include <cstdlib>
#include <exception>
using namespace std;
void my_unexpected()
{
cout << "void my_unexpected()" << endl;
throw 1;
}
void func() throw(int)
{
cout << "func()";
cout << endl;
throw 'c';
}
int main()
{
set_unexpected(my_unexpected);
try
{
func();
}
catch(int)
{
cout << "catch(int)";
cout << endl;
}
catch(char)
{
cout << "catch(char)";
cout << endl;
}
return 0;
}
g++编译器和BCC编译器结果:
func()
void my_unexpected()
catch(int)
VC编译器:
func()
catch(char)
可以看出,当在func中抛出字符型异常时(异常类型不再规格说名中),
my_unexpected()被调用,当在my_unexpected()抛出一个int类型
的异常时,这时候该异常符合函数func()的异常规格说明,则恢复程序的
正常执行,紧接着被catch捕获,否则调用调用最终的全局terminate()函数结束异常程序
8、小结