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. Edittext焦点处理

    <LinearLayout android:focusable="true" android:layout_width="0dp" android:lay ...

  2. linux(centos 6.4)下安装php memcache服务端及其客户端(详细教程)

    前言 在搭建个人博客时,由于没有使用任何框架,纯手工code前台和后台,导致遇到许多问题,其中一个问题就是mysql连接导致的页面相应速度异常低.在查询各种途径后,只能考虑使用memcache缓存.在 ...

  3. Linux基础入门

    第一节,linux系统简介 一.实验内容 了解 Linux 的历史,Linux 与 Windows 的区别等入门知识. 二.实验要求 阅读linux简介与历史 三.实验步骤 (一).Linux 为何物 ...

  4. 20135208 20135212 LINUX第一次实验报告

    北京电子科技学院(BESTI) 实     验    报     告 课程:信息安全系统设计基础                     班级: 201352 姓名:池彬宁 贺邦 学号:2013521 ...

  5. iOS开发的设计模式

    ios开发学习中,经常弄不清楚ios的开发模式,今天我们就来进行简单的总结和探讨~ (一)代理模式 应用场景:当一个类的某些功能需要由别的类来实现,但是又不确定具体会是哪个类实现. 优势:解耦合 敏捷 ...

  6. Javascript日期时间总结

    写这篇文章,总结一下前端JavaScript遇到的时间格式处理. 1 C#时间戳处理 从后台返回的C#时间为:/Date(-62135596800000)/,这个是C#的DateTime.MinVal ...

  7. C# 无法识别的转义序列

    解决这个问题头两种方法:1.加个"\"进行转义:2.在前面加个@ 示例:我要进入D盘下video文件夹中的ysxs文件夹,写法分别为: D:\\video\\ysxs @" ...

  8. [USACO2004][poj2373]Dividing the Path(DP+单调队列)

    http://poj.org/problem?id=2373 题意:一条直线分割成N(<=25000)块田,有一群奶牛会在其固定区域吃草,每1把雨伞可以遮住向左右延伸各A到B的区域,一只奶牛吃草 ...

  9. C#基础知识系列五(构造函数)

    前言 本节主要来学习下实例构造函数.私有构造函数和静态构造函数. 构造函数的作用: 1.当一个类在new实例化对象时,可调用构造函数进行初始化对象. 2.不带参数的构造函数称为“默认构造函数”. 无论 ...

  10. Javascript基础系列之(三)数据类型 (类型转化)

    所有语言都有类型转化的能力,javascript也不例外,它也为开发者提供了大量的类型转化访法,通过全局函数,可以实现更为复杂的数据类型. var a = 3; var b = a + 3; var ...