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. Drivers Dissatisfaction

    Drivers Dissatisfaction time limit per test 4 seconds memory limit per test 256 megabytes input stan ...

  2. OpenGL网络资源

    转 十大OpenGL教程 1.http://nehe.gamedev.net/这个是我觉得全世界最知名的OpenGL教程,而且有网友将其中48个教程翻译成了中文http://www.owlei.com ...

  3. java 网络编程Socket编程

    Server.java import java.io.*; import java.net.*; public class Server { public static void main(Strin ...

  4. js 基础对象二

    大的分类 JavaScript 对象 JS Array JS Boolean JS Date JS Math JS Number JS String JS RegExp JS Functions JS ...

  5. wordpress安装插件--su

     Add to Any: Subscribe Button 让读者方便的订阅你的博客到任何Feed阅读器 Google XML Sitemaps 生成完全兼容各大搜索引擎的Sitemaps/网站地图. ...

  6. 仿360新闻的热搜图片,win8风格随机九宫格布局

    360新闻地址:http://sh.qihoo.com/i/ 感觉这效果挺好的,随机九宫格,在不少地方可以用到,就研究了下他的源码,基本原理就是预先定义好几种布局模块,然后根据需要进行拼接,具体代码可 ...

  7. 我也谈 AngularJS 怎么使用Directive, Service, Controller

    原文地址:http://sunqianxiang.github.io/angularjs-zen-yao-shi-yong-directiveservicecontroller.html 其转自大漠穷 ...

  8. space 管理

    输入:表空间名字 输出:表空间下的一个文件即退出 /tmp/.sql select name from v$database; set serveroutput on; declare i1 varc ...

  9. CentOS/RHEL 7中的firewall控制

    从CentOS/RHEL 7开始firewall的使用.很多人卸载了firewall重装iptables.但是有时候只是为了开放端口什么的,没有那个闲工夫卸载重装: 永久打开一个新端口(如TCP/80 ...

  10. ural1855 Trade Guilds of Erathia

    Trade Guilds of Erathia Time limit: 2.0 secondMemory limit: 64 MB The continent of Antagarich was co ...