在分析stl源码的时候,发现几乎所有函数都包含空异常描述符throw().
那么throw()究竟有何意义呢?
其实,与其称其为"空异常描述符",还不如称其为"中断描述符".简单的说,如果throw()修饰的函数中发生任何异常,即便函数外部有捕获操作,函数也不会抛出任何异常,那么程序将直接终止!!!简单的说,对使用throw()修饰的函数进行try{}catch(...){}是没有任何意义的!顾,在使用throw()修饰函数的时候,请务必确认,函数在任何情况下都不会发送异常,顾,其大量存在stl之类非常非常严谨的代码中!!!
/*
* File: main.cpp
* Author: Vicky.H
*
*/
#include <iostream>
class myexception : public std::exception {
public:
const char* what() const throw () {
std::cout << "my exception" << std::endl;
}
};
void f1(bool flag = true) {
if (flag) throw myexception();
}
void f2(bool flag = true) throw () {
if (flag) throw myexception();
}
/*
*
*/
int main(void) {
try {
f1();
} catch (...) {
}
std::cout << "f1()抛出的异常将会被捕获,程序不会中断,将继续执行" << std::endl;
try {
f2();
} catch (...) {
}
std::cout << "f2()抛出的异常不会被捕获,程序会中断,将不会执行该条语句" << std::endl;
return 0;
}
f1()抛出的异常将会被捕获,程序不会中断,将继续执行
my exception
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
terminate called after throwing an instance of 'myexception'
what(): L3蚾`3蚾