2012-12-24 16:27 5269人阅读 评论(1) 收藏 举报
 分类:
c/c++(36) 

C++标准库异常类继承层次中的根类为exception,其定义在exception头文件中,它是C++标准库所有函数抛出异常的基类,exception的接口定义如下:

namespace std {

class exception {

public:

exception() throw();   //不抛出任何异常

exception(const exception& e) throw();

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

virtual ~exception() throw)();

virtual const char* what() const throw(); //返回异常的描述信息

};

}

除了exception类,C++还提供了一些类,用于报告程序不正常的情况,在这些预定义的类中反映的错误模型中,主要包含逻辑错误和运行时错误两大类。

逻辑错误主要包括invalid_argument, out_of_range, length_error, domain_error。当函数接收到无效的实参,会抛出invaild_argument异常,如果函数接收到超出期望范围的实参,会抛出out_of_range异常,等等。

namespace std {

class logic_error: public exception {

public:

explicit logic_error(const string &what_arg);       //关键字explicit  显式调用构造函数---见下文

};

class invalid_argument: public logic_error {

public:

explicit invalid_argument(const string &what_arg);

};

class out_of_range: public logic_error {

public:

explicit out_of_range(const string &what_arg);

};

class length_error: public logic_error {

public:

explicit length_error(const string &what_arg);

};

class domain_error: public logic_error {

public:

explicit domain_error(const string &what_arg);    //域名错误

};

}

运行时错误由程序域之外的事件引发,只有在运行时才能检测,主要包括range_error, overflow_error, underflow_error。函数可以通过抛出range_error报告算术运算中的范围错误,通过抛出overflow_error报告溢出错误。

namespace std {

class runtime_error: public exception {

public:

explicit runtime_error(const string &what_arg);

};

class range_error: public runtime_error {

public:

explicit range_error(const string &what_arg);

};

class overflow_error: public runtime_error {

public:

explicit overflow_error(const string &what_arg);

};

class underflow_error: public runtime_error {

public:

explicit underflow_error(const string &what_arg);

};

}

另外,在new头文件中定义了bad_alloc异常,exception也是bad_alloc的基类,用于报告new操作符不能正确分配内存的情形。当dynamic_cast失败时,程序会抛出bad_cast异常类,其也继承自exception类。

 
 

C++标准库异常类的更多相关文章

  1. C++异常第二篇---C++标准库异常类exception的使用

    1 继承图示 2 具体讲解 C++标准库异常类继承层次中的根类为exception,其定义在exception头文件中,它是C++标准库所有函数抛出异常的基类,exception的接口定义如下: na ...

  2. 实现C++标准库string类的简单版本

    代码如下: #ifndef STRING_H #define STRING_H #include <cassert> #include <utility> #include & ...

  3. C++ 标准库字符串类使用

    标准库中的字符串类 C++语言直接支持C语言所有概念. C++中没有原生的字符串类型. 由于C++中没有原生的字符串类型,C++标准库提供了string类型. 1.string 直接支持字符串链接 2 ...

  4. 标准库String类

    下面的程序并没有把String类的所有成员方法实现,只参考教程写了大部分重要的成员函数. [cpp] view plain copy #include<iostream> #include ...

  5. php标准库DirectoryIterator类的操作说明

    <?php $dir = new DirectoryIterator(dirname(__FILE__)); foreach ($dir as $fileInfo) { if ($fileInf ...

  6. C++ 异常机制分析(C++标准库定义了12种异常,很多大公司的C++编码规范也是明确禁止使用异常的,如google、Qt)

    阅读目录 C++异常机制概述 throw 关键字 异常对象 catch 关键字 栈展开.RAII 异常机制与构造函数 异常机制与析构函数 noexcept修饰符与noexcept操作符 异常处理的性能 ...

  7. 把《c++ primer》读薄(3-3 标准库bitset类型)

    督促读书,总结精华,提炼笔记,抛砖引玉,有不合适的地方,欢迎留言指正. //开头 #include <bitset> using std::bitset; 问题1.标准库bitset类型( ...

  8. 16.C++-初探标准库

    在别人代码里,经常看到std命名空间,比如使用std命名空间里的标准输入输出流对象cout: #include<iostream> using namespace std; int mai ...

  9. 【Python】类和对象、继承、使用文件、存储、异常、标准库(不懂)

    当你调用这个对象的方法MyObject.method(arg1, arg2)的时候,这会由Python自动转为MyClass.method(MyObject, arg1, arg2)——这就是self ...

随机推荐

  1. vbs test

    '-----------------------------------Class clsGetProfilePrivate rootDocPublic Sub setProfile(strFileN ...

  2. Android 长按Listview显示CheckBox,实现批量删除。

    ListView实现的列表,如果是可编辑,可删除的,一般都要提供批量删除功能,否则的话,一项一项的删除体验很不好,也给用户带来了很大的麻烦. 实现效果图 具体实现代码 select.xml 主布局文件 ...

  3. Activiti系列——如何在eclipse中安装 Activiti Designer插件

    这两天在评估jbpm和Activiti,需要安装一个Activiti Designer插件试用一下. 一.在线安装 从<Activiti实战>了解到可以通过如下方式安装 打开Eclipse ...

  4. [CareerCup] 2.2 Kth to Last Element of Linked List 链表的倒数第k个元素

    2.2 Implement an algorithm to find the kth to last element of a singly linked list. 这道题让我们求链表中倒数第k个元 ...

  5. [C++] 在Visual Studio工程中管理C++第三方库

    目前的项目依赖于很多第三方库,每次要再一个新的环境编译/运行,都要花很长时间先编译/安装各种第三方库,而且会出现各种问题,因此决定将所有第三方库编译好之后,放入工程的子目录中,以后就不用重复编译了. ...

  6. 第三方MMDrawerController的使用 抽屉视图+(SUNSlideSwitchView)进度条手势滑动效果实现

    下载网站:https://github.com/mutualmobile/MMDrawerController 首先,到下载网址下载MMDrawerController,将文件导入工程,里面有: MM ...

  7. Ajax基础之封装3

    今天接着我们上篇博文的栗子,现在我来扩大一下需求,之前是点击按钮取出新闻,现在要实现每隔一段事件进行新闻的更新.这个时候,肯定是加一个定时器,然后每隔一段事件,再进行一次Ajax请求,既然要经常用到A ...

  8. 【逆向怎么玩】 动态调试一款牛逼C++ IDE实录

    声明 本篇只从逆向兴趣出发,研究其程序运行原理. CLion程序版权为jetBrains所有. 注册码授权为jetBrains及其付费用户所有. 不会释出任何完整的源代码. 涉及能直接推算出注册码的地 ...

  9. 开源分布式实时计算引擎 Iveely Computing 之 WordCount 详解(3)

    WordCount是很多分布式计算中,最常用的例子,例如Hadoop.Storm,Iveely Computing也不例外.明白了WordCount在Iveely Computing上的运行原理,就很 ...

  10. 设置 git/npm/bower/pip/gem镜像或代理

    git 设置: $ git config --global http.proxy http://proxy.mysite.com 取消: $ git config --global --unset h ...