[c++] Exceptions
异常处理
一、相关资料
[ 之后,针对本章节、根据链接再进行一次系统的学习 ]
基本结构
try {
throw logic_error{"blah"};
} catch (exception) {
// caught here!
} catch (logic_error) {
// not here!
}
百家讲坛 - "必要性"
[1]
为什么需要异常机制:https://blog.csdn.net/K346K346/java/article/details/50087193
C++之父Bjarne Stroustrup在《The C++ Programming Language》中讲到:
(a) 一个库的作者可以检测出发生了运行时错误,但一般不知道怎样去处理它们(因为和用户具体的应用有关);
(b) 另一方面,库的用户知道怎样处理这些错误,但却无法检查它们何时发生(如果能检测,就可以在用户的代码里处理了,不用留给库去发现)。
Bjarne Stroustrup说:提供异常的基本目的就是为了处理上面的问题。基本思想是:让一个函数在发现了自己无法处理的错误时抛出(throw)一个异常,然后它的(直接或者间接)调用者能够处理这个问题。
[2]
C++ 引入异常的原因之一是:为了能让构造函数报错(析构函数不能抛异常这是大家都知道的常识),毕竟构造函数没有返回值,没有异常的话调用方如何得知对象构造是否成功呢?
[3]
到了异常,一般就直接让本次操作失效,保存状态好了.
定义异常
一、内置 Error类型

下表是对上面层次结构中出现的每个异常的说明:
| 异常 | 描述 |
|---|---|
| std::exception | 该异常是所有标准 C++ 异常的父类。 |
| std::bad_alloc | 该异常可以通过 new 抛出。 |
| std::bad_cast | 该异常可以通过 dynamic_cast 抛出。 |
| std::bad_exception | 这在处理 C++ 程序中无法预期的异常时非常有用。 |
| std::bad_typeid | 该异常可以通过 typeid 抛出。 |
| std::logic_error | 理论上可以通过读取代码来检测到的异常。 |
| - std::domain_error | 当使用了一个无效的数学域时,会抛出该异常。 |
| - std::invalid_argument | 当使用了无效的参数时,会抛出该异常。 |
| - std::length_error | 当创建了太长的 std::string 时,会抛出该异常。 |
| - std::out_of_range | 该异常可以通过方法抛出,例如 std::vector 和 std::bitset<>::operator[]()。 |
| std::runtime_error | 理论上不可以通过读取代码来检测到的异常。 |
| - std::overflow_error | 当发生数学上溢时,会抛出该异常。 |
| - std::range_error | 当尝试存储超出范围的值时,会抛出该异常。 |
| - std::underflow_error | 当发生数学下溢时,会抛出该异常。 |
二、自定义异常类型
通过继承和重载 exception 类来定义新的异常。
#include <iostream>
#include <exception>
using namespace std;
// 竟然是个struct
struct MyException : public exception
{
const char* what () const throw
{
return "C++ Exception";
}
}; // -------------------------------------------------
int main()
{
try
{
throw MyException();
}
catch(MyException& e)
{
std::cout << "MyException caught" << std::endl;
std::cout << e.what() << std::endl;
}
catch(std::exception& e)
{
// 其他的错误
}
}
类可以继承,结构体也可以继承.
#include <iostream>
using namespace std;
//
class MyException : public exception
{
public:
const char* what() const throw () // <----
{
std::cout << "my exception" << std::endl;
return NULL;
}
}; void f1(bool flag = true)
{
if (flag) throw MyException();
} void f2(bool flag = true) throw ()
{
if (flag) throw MyException();
}
----------------------------------------------------- int main(void)
{
try
{
f1();
}
catch (...)
{
;
}
std::cout << "f1()异常将被捕获,不会 abort,将继续执行" << std::endl; try
{
f2();
}
catch (...)
{
;
}
std::cout << "f2()异常不会被捕获,程序将会 abort,将不会执行该条语句" << std::endl; return ;
}
使用异常
Catch-by-reference is BETTER
Problems with Catch-by-value:
- inefficient due to object copying
- causes the slicing problem, and
- cannot exploit polymorphism and dynamic binding
Catch-by-reference avoids all these problems.
catch的参数是引用,自然就不需要拷贝了。
Catch-by-Value
#include <iostream>
using namespace std;
class X {
public:
X() {
std::cout << "X constructed" << std::endl;
}
X(const X &x) {
std::cout << "X copy-constructed" << std::endl;
}
~X() {
std::cout << "X destructed" << std::endl;
}
};
void g() {
throw X{};
}
void f() {
try {
g();
} catch (X x) { // 改为catch(X &x), 其他同理
std::cout << "caught in f; rethrow" << std::endl;
throw;
}
}
int main()
{
cout << "Hello World!" << endl;
try {
f();
} catch (X x) { // 这里又是一次 浪费时间的 copy
std::cout << "caught in main" << std::endl;
}
return ;
}
Result:

Catch-by-Reference
改为reference后,concise了不少!

Stack Unwinding

关于new object这件事上,Obversely, this avoids memory leaks but is messy.
便有了Smart Pointer? 这里是初始化列表的用法。

Stack Exception Safety
如果noexcept修饰的函数抛出了异常,编译器可以选择直接调用std::terminate()函数来终止程序的运行,这比基于异常机制的throw()在效率上会高一些。
这是因为异常机制会带来一些额外开销,比如函数抛出异常,会导致函数栈被依次地展开(unwind),并依帧调用在本帧中已构造的自动变量的析构函数等。
Common Levels of Exception Safety

End.
[c++] Exceptions的更多相关文章
- "NHibernate.Exceptions.GenericADOException: could not load an entity" 解决方案
今天,测试一个项目的时候,抛出了这个莫名其妙的异常,然后就开始了一天的调试之旅... 花了很长时间,没有从代码找出任何问题... 那么到底哪里出问题呢? 根据下面那段长长的错误日志: -- ::, ...
- 2000条你应知的WPF小姿势 基础篇<34-39 Unhandled Exceptions和Resource>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'dd' in 'where clause'
今天在使用mysql数据库查找数据的时候报错,错误信息如下: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown co ...
- MIT 6.828 JOS学习笔记18. Lab 3.2 Part B: Page Faults, Breakpoints Exceptions, and System Calls
现在你的操作系统内核已经具备一定的异常处理能力了,在这部分实验中,我们将会进一步完善它,使它能够处理不同类型的中断/异常. Handling Page Fault 缺页中断是一个非常重要的中断,因为我 ...
- Solve VS2010 Error "Exceptions has been thrown by the target of an invocation"
Sometimes when you open a VS2010 project, an error window will pop up with the error message "E ...
- MyBatis-Exception:org.apache.ibatis.exceptions.PersistenceException
错误信息如下: HTTP Status 500 - org.mybatis.spring.MyBatisSystemException: nested exception is org.apache. ...
- App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file
ios进行http请求,会出现这个问题: App Transport Security has blocked a cleartext HTTP (http://) resource load sin ...
- 添加 All Exceptions 断点后, 每次运行都会在 main.m 中断的一种解决方法
在本人项目添加导入和使用新的字体过程中,遇到一个很奇怪的问题: 项目开启了全局断点,但是每次启动都会运行在mian.m中断,点击下一步程序继续正常运行. 不知道是什么原因,于是google百度寻找答案 ...
- python基于Django框架编译报错“django.core.exceptions.ImproperlyConfigured”的解决办法?
下面是我具体遇到的问题和解决方法: 错误详细信息: django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_IND ...
- Rethrowing exceptions and preserving the full call stack trace
refer:http://weblogs.asp.net/fmarguerie/archive/2008/01/02/rethrowing-exceptions-and-preserving-the- ...
随机推荐
- dedecms功能性函数封装(XSS过滤、编码、浏览器XSS hack、字符操作函数)
dedecms虽然有诸多漏洞,但不可否认确实是一个很不错的内容管理系统(cms),其他也不乏很多功能实用性的函数,以下就部分列举,持续更新,不作过多说明.使用时需部分修改,你懂的 1.XSS过滤. f ...
- [IOS]swift 使用AVOS的API
避免写后台代码,但保证app能够有后台,使用了AVOS这个产品来帮助自己的app联网,AVOS是很强大的云服务,基本原理和Parse差不多,只是parse在国外可能对国内用户联网速度上有点限制. 下面 ...
- vue.js 的学习
官方网站(作者居然是中国人) http://vuejs.org/ Vuex中文手册 http://vuex.vuejs.org Vue-Router 手册 http://router.vuejs.or ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
- 用jdbc访问二进制类型的数据
package it.cast.jdbc; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; impor ...
- TcpClient 有好多坑
下面2篇文章里头的问题都碰到了,真是好坑哈, 在此留念. 使用 TcpClient 與 NetworkStream 類別開發時的注意事項 [C#] NetworkStream.Write()存在严重b ...
- Unit Testing with NSubstitute
These are the contents of my training session about unit testing, and also have some introductions a ...
- 使用C#给Linux写Shell脚本(下篇)
在上篇的<使用C#给Linux写Shell脚本>结尾中,我们留下了一个关于C#如何调用BashShell的问题.在文章发布之后,我留意到有读者留言推荐使用“Pash”(一款类PowerSh ...
- 在互联网公司参与拍卖是一种怎样的感觉?part 1
拍卖在中国是不太流行的一件事,为什么呢?说不太出.当初在外国火的不得了的ebay在交易时采用的就是拍卖出价的模式,但进入中国后这种方式就是玩不转,不得以后来也变成了跟淘宝一样的一口价方式. 话说现在每 ...
- AngularJS 源码分析2
上一篇地址 本文主要分析RootScopeProvider和ParseProvider RootScopeProvider简介 今天这个rootscope可是angularjs里面比较活跃的一个pro ...