C++Primer读书笔记(十二)

来源:互联网 发布:mac 命令行删除文件 编辑:程序博客网 时间:2024/04/28 05:11

C++中的标准异常类


namespace std

{

    //exception派生

    class logic_error; //逻辑错误,在程序运行前可以检测出来


    //logic_error派生

    class domain_error; //违反了前置条件

    class invalid_argument; //指出函数的一个无效参数

    class length_error; //指出有一个超过类型size_t的最大可表现值长度的对象的企图

    class out_of_range; //参数越界

    class bad_cast; //在运行时类型识别中有一个无效的dynamic_cast表达式

    class bad_typeid; //报告在表达试typeid(*p)中有一个空指针p

   

    //exception派生

    class runtime_error; //运行时错误,仅在程序运行中检测到

   

    //runtime_error派生

    class range_error; //违反后置条件

    class overflow_error; //报告一个算术溢出

    class bad_alloc; //存储分配错误

}

 

请注意观察上述类的层次结构,可以看出,标准异常都派生自一个公共的基类exception。基类包含必要的多态性函数提供异常描述,可以被重载。下面是exception类的原型:

class exception

{

public:

    exception() throw();

    exception(const exception& rhs) throw();

    exception& operator=(const exception& rhs) throw();

    virtual ~exception() throw();

    virtual const char *what() const throw();

};

其中的一个重要函数为what(),它返回一个表示异常的字符串指针。

原创粉丝点击