std::
<bad_exception>
类 <exception>
意外处理程序抛出的异常
exception←
bad_exception
这是一种特殊类型的异常,专门设计用于在函数的动态异常说明符中列出(例如,在其抛出说明符中)。
如果发生非预期的异常(函数抛出动态异常说明符(exception specification)以外的异常),
程序会自动调用unexpected(),如果用set_unexpected自定义了unexpected函数,
自定义unexpected函数中有throw;或抛出了不在动态异常说明符中的其他异常,c++运行时环境将自动抛出bad_exception。
它的成员what返回一个标识异常的以空结尾的字符序列。
在current_exception的某些实现下(从c++ 11开始),抛出此异常表示复制异常对象的尝试失败。
☞ 兼容性
不赞成使用动态异常说明符(自c++ 11以来)。
☣ 示例
// bad_exception example
#include <iostream> // std::cerr
#include <exception> // std::bad_exception, std::set_unexpected
void myunexpected () {
std::cerr << "unexpected handler called\n";
throw;
}
void myfunction () throw (int,std::bad_exception) {
throw 'x'; // throws char (not in exception-specification)
}
int main (void) {
std::set_unexpected (myunexpected);
try {
myfunction();
}
catch (int) { std::cerr << "caught int\n"; }
catch (std::bad_exception be) { std::cerr << "caught bad_exception\n"; }
catch (...) { std::cerr << "caught some other exception\n"; }
return 0;
}
|
输出:
unexpected handler called
caught bad_exception
☂ 异常安全性
无抛出保证:没有成员抛出异常。
🍄 另请参阅