std::
<exception>
类 <exception>
标准异常类
标准异常的基类。
标准库组件抛出的所有对象都派生自这个类。因此,所有标准异常类型都可以通过引用捕获。
它被声明为:
-
C++98
-
class exception {
public:
exception () throw();
exception (const exception&) throw();
exception& operator= (const exception&) throw();
virtual ~exception() throw();
virtual const char* what() const throw();
} |
-
C++11
-
class exception {
public:
exception () noexcept;
exception (const exception&) noexcept;
exception& operator= (const exception&) noexcept;
virtual ~exception();
virtual const char* what() const noexcept;
} |
☞ 成员函数
☞ 派生类型(分散在不同的库头文件中)
间接(通过logic_error):
间接(通过 runtime_error):
间接(通过 bad_alloc):
间接(通过 system_error,始于c++ 11):
☣ 示例
// exception example
#include <iostream> // std::cerr
#include <typeinfo> // operator typeid
#include <exception> // std::exception
class Polymorphic {virtual void member(){}};
int main () {
try
{
Polymorphic * pb = 0;
typeid(*pb); // throws a bad_typeid exception
}
catch (std::exception& e)
{
std::cerr << "exception caught: " << e.what() << '\n';
}
return 0;
}
|
可能输出:
exception caught: St10bad_typeid