异常 exception类
exception类
该类通常作为异常的基类 其中有一个what()虚函数 可被重新定义
ep:
#include <exception>
class bad_mean : public exception
{
public:
vitrual const char * what() {return "bad bad vad";}
};
如不想用不同方法处理派生而来的异常 则可以在一个catch块中捕获
catch (exception & exc)
{
cout<<exc.what()<<endl;
…
}
///////////////////////////////////////////////////////////////////////////////////////////
stdexcept异常类
头文件#include <stdexcept>中定义
该类又派生出两种大范围类 logic_error 和 runtime_error
logic_error:典型逻辑错误,该类可以被发现并修改,可以在编程是避免
runtime_error:在运行时难以预料和防范的错误
logic_error-----------每个类都有类似传string型的构造函数 并有what方法
domain_error:定义域错误异常
invalid_argument:接受意外参数异常
lenght_error:没有足够空间执行下面操作
out_of_bounds:下标索引错误
runtime_error----------每个类都有类似传string型的构造函数 并有what方法
range_error:非上下溢出
overflow_error:数值取值范围上溢
underflow_error:数值取值范围下溢
可以这样设计捕获
try
{
……
}
catch(out_of_bounds & oe)
{……}
catch(logic_error & oe)
{……}
catch(exception & oe)
{……}


浙公网安备 33010602011771号