unexpected exception问题
c++ primer plus中关于意外异常有以下解说:
通过set_unecpected函数设置出现意外异常时的处理函数,处理函数可以引发新的异常:
如果新引发的异常与原来的异常规范匹配,则程序将从哪里开始执行,即寻找与新引发的异常相匹配的
catch块。
如果新引发的异常与原来的异常规范不匹配,且原来的异常规范中包含了std::bad_exception类型,则
不匹配的异常将被std::bad_exception异常所取代。
照上所述,以下两段代码应该正常运行,可实际却都异常终止了,why??
#include <exception>
#include <new>
#include <iostream>
using namespace std;
void fn(int i) throw(int)
{
if (i) cout<<"calling fn()!";
else throw float();
}
void myUnexpect()
{
throw int();//抛出int,被捕获??
}
void main()
{
set_unexpected(myUnexpect);
try {
fn(0);
}
catch(int)
{
cout<<"catching int!";
}
}
#include <exception>
#include <new>
#include <iostream>
using namespace std;
void fn(int i) throw(int,bad_exception)
{
if (i) cout<<"calling fn()!";
else throw float();
}
void myUnexpect()
{
throw char();
}
void main()
{
set_unexpected(myUnexpect);
try {
fn(0);
}
catch(int)
{
cout<<"catching int!";
}
catch(bad_exception)
{
cout<<"catching bad_exception!";
}
}