1 继承图示

2 具体讲解

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);

};

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_eroor报告算术运算中的范围错误,通过抛出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类。

转载自:http://blog.csdn.net/zhq651/article/details/8425579

3  例子

#include<stdexcept>

try

  {

  throw invalid_argument("hah");

  //throw out_of_range(" we get the outofrange error");

   }catch(invalid_argument& in)

  {

  cout<<"we catch invalid_argument"<<in.what()<<endl;

  }catch(...)

  {

  cout<<"we catch unexpected error,exit"<<endl;

  return 1;

  }

C++异常第二篇---C++标准库异常类exception的使用的更多相关文章

  1. C++标准库异常类

    C++标准库异常类 2012-12-24 16:27 5269人阅读 评论(1) 收藏 举报  分类: c/c++(36)  C++标准库异常类继承层次中的根类为exception,其定义在excep ...

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

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

  3. 《C++ Primer》学习笔记【第二部分 C++标准库】

    第8章 IO库 IO对象不能复制,即1.IO对象不能存储在vector或其他容器中   2.如果需要传递或返回IO对象,必须传递或返回指向该对象的指针或引用. 一般情况下,如果要传递IO对象以便对它进 ...

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

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

  5. 第二篇:请求库之requests,selenium

    requests模块 一.介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:reques ...

  6. 第二篇:MySQL库相关操作

    一 系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等performance_schema: MyS ...

  7. 【磁盘/文件系统】第二篇:标准磁盘分区流程针对fdisk(硬盘容量小于2T且分区数不能大于15个分区)

    前奏:先插上一块磁盘(做了RAID的磁盘才能分区) 查看磁盘连上服务器没有,或者说是可以进行分区前奏 [root@PandaLi ~]# ll /dev/sd sda sda1 sda2 sda3 s ...

  8. 【SSH三大框架】Hibernate基础第二篇:编写HibernateUtil工具类优化性能

    相对于上一篇中的代码编写HibernateUtil类以提高程序的执行速度 首先,仍然要写一个javabean(User.java): package cn.itcast.hibernate.domai ...

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

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

随机推荐

  1. input内文字点击消失 弹出层,可以写表单

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  2. linux的学习系列 7---管道和过滤器

    有时候,我们可以把两个命令连起来使用,一个命令的输出作为另一个命令的输入,这就叫做管道.为了建立管道,需要在两个命令之间使用竖线(|)连接. 管道是Linux进程之间一种重要的通信机制:除了管道,还有 ...

  3. Got Stucked in C++ Static Library Loading.. for some time

    I used to load library using 1 single .dll file, so when I happen to do method calling between 2 pro ...

  4. Alamofire 框架浅析

    下面是 Github 主页上对 Alamofire 的描述 Elegant HTTP Networking in Swift 为什么这次我选择阅读 Alamofire 的源代码而不是 AFNetwor ...

  5. FZU 1912 Divisibility by Thirty-six(整除问题+字符串维护+贪心)

    这个整除36的与整除45的完全一样,就是被4整除的有点多,但都是两位数,所以枚举后面两位就可以了. #include <stdio.h> #include <string.h> ...

  6. 基于VirtualBox 安装和配置Fuel OpenStack(V6.1)

    1.环境准备 准备一台内存较大的主机,12G以上 下载安装VirtualBox及其匹配的扩展包 virtualbox: http://download.virtualbox.org/virtualbo ...

  7. Android Studio相关的坑

    html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...

  8. Windows API 之 CreateFile、CreateFileMapping 、MapViewOfFile

    CreateFile Creates or opens a file or I/O device. The most commonly used I/O devices are as follows: ...

  9. Chapter 1 First Sight——20

    After two classes, I started to recognize several of the faces in each class. 两节课之后,我开始记住了每节课的那几张脸. ...

  10. HDU 3726 Graph and Queries 平衡树+前向星+并查集+离线操作+逆向思维 数据结构大综合题

    Graph and Queries Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...