[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- ...
随机推荐
- Python实战 :2017国考职业表excel转数据库,再查询生成excel
最近看2017年国考的职业表,多而杂,不好过滤我想要的信息,特此把它转成Sqlite3数据库,再从数据库里把查询结果导出成excel,方便找职业. (后附上整套代码) 环境:python2.7 x ...
- iOS 添加中文支持的操作
1.选择工程菜单,这里要选中Project,而不是Targets 2.点击Info菜单, 下拉到最后,看到Localizations. 点击+号. 3.选择中文 chinese-simplif ...
- webpack配置ES6 + react套装开发
配置ES6 1.安装插件: babel-loader,babel-preset-es2015,babel-preset-react. 2.添加配置文件中部分内容: { test: /\.js$/, l ...
- cocoapods安装出错问题
今天执行pod install时,出现了错误,提示更新,好,那就更新; 1.终端执行了下:gem sources -l 查看了下源 *** CURRENT SOURCES *** https:// ...
- DSY2933*地图
Description 一个人口统计办公室要绘制一张地图.由于技术的原因只能使用少量的颜色.两个有相同或相近人口的区域在地图应用相同的颜色.例如一种颜色k,则A(k) 是相应的数,则有: 在用颜色 ...
- css 清除浮动 clear
.clearfix{ zoom:1;/*对于老版本的IE进行兼容的设置*/ } .clearfix:after{ content:""; display:block; visibi ...
- Comparing the MSTest and Nunit Frameworks
I haven't seen much information online comparing the similarities and differences between the Nunit ...
- TCP和UDP的区别
(1)TCP是面向连接的传输控制协议,而UDP提供了无连接的数据报服务:(2)TCP具有高可靠性,确保传输数据的正确性,不出现丢失或乱序:UDP在传输数据前不建立连接,不对数据报进行检查与修改,无须等 ...
- ABP理论学习之Nuget包
返回总目录 本篇目录 框架 测试基 ABP已经发布在Nuget上,这里是所有包的列表. 框架 Abp Abp系统的核心包.所有其他的包都依赖这个包. Abp.Web 提供了MVC和Web API都使用 ...
- Hadoop学习笔记—17.Hive框架学习
一.Hive:一个牛逼的数据仓库 1.1 神马是Hive? Hive 是建立在 Hadoop 基础上的数据仓库基础构架.它提供了一系列的工具,可以用来进行数据提取转化加载(ETL),这是一种可以存储. ...