目录1.异常的概念和使用1.1 异常的概念1.2 异常的抛出和捕获1.3 栈展开1.4 查找匹配的处理代码1.5 异常重新抛出1.6 异常安全问题1.7 异常规范2. 标准库的异常1.异常的概念和使用1.1 异常的概念C语言阶段主要是通过错误码的形式处理错误错误码本质就是对错误信息进行分类编号拿到错误码以后还要去查询错误信息比较麻烦。异常时抛出一个对象这个对象可以涵盖更全面的个证信息。1.2 异常的抛出和捕获程序出现问题时我们通过(throw)抛出一个对象来引发一个异常该对象的类型以及当前的调用链决定了应该由哪个catch的处理代码来处理该异常。被选中的处理代码是调用链中与该对象类型匹配且离抛出异常位置最近的那一个。根据抛出对象的类型和内容程序的抛出一场部分告知异常处理部分到底发生了什么错误。当throw执行时throw后面的语句将不再被执行。程序的执行从throw位置跳到最近的与之匹配的catch模块catch可能是同一函数中的一个局部的catch也可能时调用链中另一个函数的中的catch控制权从throw位置转移到了catch位置。这里还有两个重要的含义1、沿着调用链的函数可能提早退出。2、一旦程序开始执行异常出路程序沿着调用链创建的对象都将销毁。抛出异常对象后会生成一个异常对象的拷贝因为抛出的异常对象可能是一个局部对象所以会生成一个拷贝对象这个拷贝的对象会在catch子句后销毁。这里的处理类似于函数的传值返回catch代码是通过throw跳过去的否则话是不会执行的没有发生异常或者不匹配就直接跳过这个catchdouble Divide(int a, int b) { try { //当b0时抛出异常 if (b 0) { string s(Divide by zero condition!); throw s; } else { return ((double)a / (double)b); } } catch (int errid) { cout errid endl; } } void Func() { int len, time; cin len time; try { cout Divide(len, time) endl; } catch (const char* errmsg) { cout errmsg endl; } cout __FUNCTION__ : __LINE__ 行执行 endl; } int main() { while (1) { try { Func(); } catch (const string errmsg) { cout errmsg endl; } } return 0; }输入1 和 0后抛异常当throw执行时throw后面的语句将不再被执行。程序的执行从throw位置跳到最近的与之匹配的catch模块1.3 栈展开抛出异常后程序暂停当前函数的执行开始寻找与之匹配的catch子句首先检查throw本⾝是否在try块内部如果在则查找匹配的catch语句如果有匹配的则跳到catch的地方进行处理。throw本身并没有在try块内部即使catch内部类型匹配同样也会终止程序。int main() { while (1) { Func(); try { int x 0; cout x endl; } catch (const string errmsg) { cout errmsg endl; } } return 0; }如果当前函数中没有try/catch子句或者有try/catch子句但是类型不匹配则退出当前函数继续在外层调用函数链中查找上述查找的catch过程被称为栈展开。如果到达main函数依旧没有找到匹配的catch子句程序会调用标准库的 terminate 函数终止程序。如果找到匹配的catch子句处理后catch子句代码会继续执⾏。double Divide(int a, int b) { try { //当b0时抛出异常 if (b 0) { string s(Divide by zero condition!); throw s; } else { return ((double)a / (double)b); } } catch (int errid) { cout errid endl; } } void Func() { int len, time; cin len time; try { cout Divide(len, time) endl; } catch (const char* errmsg) { cout errmsg endl; } cout __FUNCTION__ : __LINE__ 行执行 endl; } int main() { while (1) { try { Func(); } catch (const char* errmsg) { cout errmsg endl; } } return 0; }没有与之对应的类型的catch此时就会中止程序。异常一定要被捕获到。1.4 查找匹配的处理代码⼀般情况下抛出对象和catch是类型完全匹配的如果有多个类型匹配的就选择离他位置更近的那个。但是也有⼀些例外允许从非常量向常量的类型转换也就是权限缩小允许数组转换成指向数组元素类型的指针函数被转换成指向函数的指针允许从派生类向基类类型的转换这个点非常实用实际中继承体系基本都是用这个方式设计的。#includethread //一般大型项目程序才会使用异常下面我们模拟设计一个服务的几个模块 //每个模块的继承都是Exception的派生类每个模块可以添加自己的数据 //最后捕获时我们捕获基类就可以 class Exception { public: Exception(const string errmsg, int id) :_errmsg(errmsg) , _id(id) { } virtual string what() const { return _errmsg; } int getid() const { return _id; } protected: string _errmsg; //错误描述 int _id; //错误码 }; class SqlException : public Exception { public: SqlException(const string errmsg, int id, const string sql) :Exception(errmsg, id) , _sql(sql) {} //记录日志 virtual string what() const { string str SqlException:; str _errmsg; str -; str _sql; return str; } private: const string _sql; }; class CacheException : public Exception { public: CacheException(const string errmsg, int id) :Exception(errmsg, id) { } virtual string what() const { string str CacheException:; str _errmsg; return str; } }; class HttpException : public Exception { public: HttpException(const string errmsg, int id, const string type) :Exception(errmsg, id) , _type(type) { } virtual string what() const { string str HttpException:; str _type; str :; str _errmsg; return str; } private: const string _type; }; void SQLMgr() { if (rand() % 7 0) { throw SqlException(权限不足, 100, select * from name 张三); } else { cout SQLMgr 调用成功 endl; } } void CacheMgr() { if (rand() % 5 0) { throw CacheException(权限不足, 100); } else if (rand() % 6 0) { throw CacheException(数据不存在, 101); } else { cout CacheMgr 调用成功 endl; } SQLMgr(); } void HttpServer() { if (rand() % 3 0) { throw HttpException(请求资源不存在, 100, get); } else if (rand() % 4 0) { throw HttpException(权限不足, 101, post); } else { cout HttpServer调用成功 endl; } CacheMgr(); } int main() { srand(time(0)); while (1) { this_thread::sleep_for(chrono::seconds(1)); try { HttpServer(); } catch (const Exception e) // 这里捕获基类基类对象和派生类对象都可以被捕获 { //多态 Exception父类的引用 cout e.what() endl; //记录异常日志 } //捕获任意类型的异常,增强程序的健壮性防止有人乱抛异常导致程序终止 catch (...) { cout 未知异常 endl; } } return 0; }如果到main函数异常仍旧没有被匹配就会终止程序不是发⽣严重错误的情况下我们是不期望程序终止的所以⼀般main函数中最后都会使⽤catch(...)它可以捕获任意类型的异常但是是不知道异常错误是什么。1.5 异常重新抛出有时catch到⼀个异常对象后需要对错误进⾏分类其中的某种异常错误需要进行特殊的处理其他错误则重新抛出异常给外层调用链处理。捕获异常后需要重新抛出直接 throw; 就可以把捕获的对象直接抛出。#includethread //一般大型项目程序才会使用异常下面我们模拟设计一个服务的几个模块 //每个模块的继承都是Exception的派生类每个模块可以添加自己的数据 //最后捕获时我们捕获基类就可以 class Exception { public: Exception(const string errmsg, int id) :_errmsg(errmsg) , _id(id) { } virtual string what() const { return _errmsg; } int getid() const { return _id; } protected: string _errmsg; //错误描述 int _id; //错误码 }; class SqlException : public Exception { public: SqlException(const string errmsg, int id, const string sql) :Exception(errmsg, id) , _sql(sql) {} //记录日志 virtual string what() const { string str SqlException:; str _errmsg; str -; str _sql; return str; } private: const string _sql; }; class CacheException : public Exception { public: CacheException(const string errmsg, int id) :Exception(errmsg, id) { } virtual string what() const { string str CacheException:; str _errmsg; return str; } }; class HttpException : public Exception { public: HttpException(const string errmsg, int id, const string type) :Exception(errmsg, id) , _type(type) { } virtual string what() const { string str HttpException:; str _type; str :; str _errmsg; return str; } private: const string _type; }; void SQLMgr() { if (rand() % 7 0) { throw SqlException(权限不足, 100, select * from name 张三); } else { cout SQLMgr 调用成功 endl; } } void CacheMgr() { if (rand() % 5 0) { throw CacheException(权限不足, 100); } else if (rand() % 6 0) { throw CacheException(数据不存在, 101); } else { cout CacheMgr 调用成功 endl; } SQLMgr(); } void HttpServer() { if (rand() % 3 0) { throw HttpException(请求资源不存在, 100, get); } else if (rand() % 4 0) { throw HttpException(权限不足, 101, post); } else { cout HttpServer调用成功 endl; } CacheMgr(); } //下面程序模拟展示了聊天时发送信息发送失败捕获异常但是可能在电梯地下室等场景 //手机信号不好则需要多次尝试如果多次尝试都发送不出去则就需要捕获异常再重新 //抛出其次如果不是网络差导致的错误捕获后也要重新抛出。 void _SeedMsg(const string s) { //模拟的 if (rand() % 2 0) { throw HttpException(网络不稳定发送失败, 102, put); } else if (rand() % 7 0) { throw HttpException(你已经不是对象的好友发送失败, 103, put); } else { cout 发送成功 endl; } } void SendMsg(const string s) { // 发送消息失败则再重试3次 for (size_t i 0; i 4; i) { try { //封装_SeedMsg _SeedMsg(s); break; } catch (const Exception e) { // 捕获异常if中是102号错误网络不稳定则重新发送 // 捕获异常else中不是102号错误则将异常重新抛出 if (e.getid() 102) { // 重试三次以后否失败了则说明网络太差了重新抛出异常 if (i 3) throw e; //throw 捕获到什么就抛什么 cout 开始第 i 1 重试 endl; } else { throw e; //throw 捕获到什么就抛什么 } } } } int main() { srand(time(0)); string str; while (cin str) { try { SendMsg(str); } catch (const Exception e) { cout e.what() endl endl; } catch (...) { cout Unkown Exception endl; } } return 0; }1.6 异常安全问题异常抛出后后⾯的代码就不再执行前⾯申请了资源(内存、锁等)后面进行释放但是中间可能会抛异常就会导致资源没有释放这里由于异常就引发了资源泄漏产生安全性的问题。中间我们需要捕获异常释放资源后面再重新抛出当然后面会提到智能指针有关的文章里面的RAII方式解决这种问题是更好的。其次析构函数中如果抛出异常也要谨慎处理比如析构函数要释放10个资源释放到第5个时抛出异常则也需要捕获处理否则豁免的5个资源就没释放也资源泄漏了。《Effctive C》第8个条款也专门讲了这个问题别让异常逃离析构函数。在析构函数里面抛异常也是一个坑。double Divide(int a, int b) { // 当b 0时抛出异常 if (b 0) { throw Division by zero condition!; } return (double)a / (double)b; } void Func() { // 这里可以看到如果发生除0错误抛出异常另外下面的array没有得到释放。 int* array new int[10]; int len, time; cin len time; cout Divide(len, time) endl; cout delete [] array endl; delete[] array; } int main() { try { Func(); } catch (const char* errmsg) { cout errmsg endl; } catch (const exception e) { cout e.what() endl; } catch (...) { cout Unkown Exception endl; } return 0; }运行结果这样是没有问题的资源得到了释放运行结果此时就是有问题的资源并没有得到释放该如何解决上述内存泄露的问题呢void Func() { // 这里可以看到如果发生除0错误抛出异常另外下面的array没有得到释放。 // 所以这里捕获异常后并不处理异常异常还是交给外层处理这里捕获了再 // 重新抛出去。 int* array new int[10]; try { int len, time; cin len time; cout Divide(len, time) endl; } catch (...) { cout delete [] array endl; delete[] array; throw; //重新抛出捕获到什么就抛什么异常 } cout delete [] array endl; delete[] array; }此时即使是抛异常也会对对应的资源进行释放1.7 异常规范对于用户和编译器而言预先知道某个程序会不会抛出异常⼤有裨益知道某个函数是否会抛出异常有助于简化调⽤函数的代码。C98中函数参数列表的后⾯接throw()表示函数不抛异常函数参数列表的后面接throw(类型1,类型2...)表示可能会抛出多种类型的异常可能会抛出的类型⽤逗号分割。C98的这种方式过于复杂实践中并不好用C11中进行了简化函数参数列表后面加noexcept表示不会抛出异常啥都不加表示可能会抛出异常。​ // C98 // 这里表示这个函数智慧抛出bad_alloc的异常 void* operator new (std::size_t size) throw (std::bad_alloc); //std::bad_alloc是库里面的一个exception里面的一个派生类 // 这里表示这个函数不会抛出异常 void* operator delete (std::size_t size, void* ptr) throw(); // C11 size_type size() const noexcept; //确定在某个接口是不会抛异常的加上noexcept例如vector::begin接口 iterator begin() noexcept; const_iterator begin() const noexcept;编译器并不会在编译时检查noexcept也就是说如果⼀个函数用noexcept修饰了但是同时又包含了throw语句或者调用的函数可能会抛出异常编译器还是会顺利编译通过的(有些编译器可能会报个警告)。但是⼀个声明了noexcept的函数抛出了异常程序会调用 terminate 终止程序。double Divide(int a, int b) noexcept { // 当b 0时抛出异常 if (b 0) { throw Division by zero condition!; } return (double)a / (double)b; }但是⼀个声明了noexcept的函数抛出了异常程序会调用 terminate 终止程序。noexcept(expression)还可以作为⼀个运算符去检测⼀个表达式是否会抛出异常可能会则返回false不会就返回true。double Divide(int a, int b) { // 当b 0时抛出异常 if (b 0) { throw Division by zero condition!; } return (double)a / (double)b; } void Func() { // 这里可以看到如果发生除0错误抛出异常另外下面的array没有得到释放。 // 所以这里捕获异常后并不处理异常异常还是交给外层处理这里捕获了再 // 重新抛出去。 int* array new int[10]; try { int len, time; cin len time; cout Divide(len, time) endl; } catch (...) { cout delete [] array endl; delete[] array; throw; //重新抛出捕获到什么就抛什么异常 } cout delete [] array endl; delete[] array; } int main() { int i 0; cout noexcept(Divide(1,2)) endl; cout noexcept(Divide(1,0)) endl; cout noexcept(i) endl; return 0; }2. 标准库的异常C标准库也定义了⼀套⾃⼰的⼀套异常继承体系库基类是exception所以我们⽇常写程序需要在主函数捕获exception即可要获取异常信息调⽤what函数what是⼀个虚函数派⽣类可以重写。一般写3个捕获异常的一个自己写的一个标准库的一个三个点的catch(...)就够用了std::bad_alloc 是 std::exceptiond的派生类所以捕获exception就可以了