C++异常第二篇---C++标准库异常类exception的使用
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的使用的更多相关文章
- C++标准库异常类
C++标准库异常类 2012-12-24 16:27 5269人阅读 评论(1) 收藏 举报 分类: c/c++(36) C++标准库异常类继承层次中的根类为exception,其定义在excep ...
- 实现C++标准库string类的简单版本
代码如下: #ifndef STRING_H #define STRING_H #include <cassert> #include <utility> #include & ...
- 《C++ Primer》学习笔记【第二部分 C++标准库】
第8章 IO库 IO对象不能复制,即1.IO对象不能存储在vector或其他容器中 2.如果需要传递或返回IO对象,必须传递或返回指向该对象的指针或引用. 一般情况下,如果要传递IO对象以便对它进 ...
- C++ 标准库字符串类使用
标准库中的字符串类 C++语言直接支持C语言所有概念. C++中没有原生的字符串类型. 由于C++中没有原生的字符串类型,C++标准库提供了string类型. 1.string 直接支持字符串链接 2 ...
- 第二篇:请求库之requests,selenium
requests模块 一.介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:reques ...
- 第二篇:MySQL库相关操作
一 系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等performance_schema: MyS ...
- 【磁盘/文件系统】第二篇:标准磁盘分区流程针对fdisk(硬盘容量小于2T且分区数不能大于15个分区)
前奏:先插上一块磁盘(做了RAID的磁盘才能分区) 查看磁盘连上服务器没有,或者说是可以进行分区前奏 [root@PandaLi ~]# ll /dev/sd sda sda1 sda2 sda3 s ...
- 【SSH三大框架】Hibernate基础第二篇:编写HibernateUtil工具类优化性能
相对于上一篇中的代码编写HibernateUtil类以提高程序的执行速度 首先,仍然要写一个javabean(User.java): package cn.itcast.hibernate.domai ...
- php标准库DirectoryIterator类的操作说明
<?php $dir = new DirectoryIterator(dirname(__FILE__)); foreach ($dir as $fileInfo) { if ($fileInf ...
随机推荐
- The magic behind configure, make, make install
原文:https://robots.thoughtbot.com/the-magic-behind-configure-make-make-install#where-do-these-scripts ...
- cmd实用指令
具体可参考:http://www.jb51.net/os/windows/36986.html 以下是本人总结的一些比较实用的指令,仅仅只是自己的实战笔记 f: 进入F盘 同理 c: 进入C盘 cd ...
- 【裸最小生成树】 模板 poj 1258
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #def ...
- apicloud本地测试安卓测试包安装
1.liutingdeMacBook-Pro:~ js-lt$ which adb 2.liutingdeMacBook-Pro:~ js-lt$ ls 3.liutingdeMacBook-Pro: ...
- gameUnity 0.15 beta 网络游戏框架
增加了 换人 和换衣服 ,加入了动画事件.beta版本 0.15测试版本 目前就到此,正式版本 会在7月底 和 0.2版本一起推出.届时,换装系统 将不仅仅是 换装备,而是有 换装后 打怪的体验,更 ...
- QT修改默认的滚动条样式
这几天写一个类似于悬浮窗的小程序,可是qt自带的滚动条实在难看,经过多番查找终于找到一个类似于qq聊天窗口的滚动条,废话不说上代码.希望能帮到大家 1.写入到文件中,新建个xx.qss,然后复制一下内 ...
- Python CGI编程和CGIHTTPServer
Python2.7 的CGIHTTPServer 可以作为一个简单的HTTP服务器,能够调用cgi脚本 1 在任意目录下创建一个特殊的目录 cgi-bin ,用于存放自己写的脚本(.py或.cgi) ...
- magento head.phtml 加载<a target=_parent
<script language="javascript">function alinks(){ var links = document.getElementsBy ...
- JSP文件上传--FileUpload组件
如果使用上传操作,并且没有使用框架之类,最好使用Smartupload,因为FileUpdate太难使用. 下载组件: fileupload包:http://commons.apache.org/pr ...
- VLAN及Trunk实验
VLAN及Trunk实验 一 基本VLAN试验 实验拓扑: 实验目的:掌握VLAN基本配置 掌握静态接口VLAN划分 实验需求:在交换机switch上面创建两个VLAN,vlan2和vlan3,vla ...