C++——C++异常处理
1.C内置了异常处理的语法元素 try...catch...try语句处理正常代码逻辑catch语句处理异常情况try语句的异常由对应的catch语句处理C通过throw语句抛出异常信息2.C异常处理分析throw抛出的异常必须被catch处理1当前函数能够处理异常程序继续向下执行2当前函数无法处理异常则函数停止执行并返回#includeiostream #includestring using namespace std; double divide(double a, double b) { const double delta 0.0000000000001; double ret 0; if (!((-delta b) (b delta))) { ret a / b; } else { throw 0; } return ret; } int main() { try { double r divide(1, 1); cout r r endl; } catch (...) { cout 除数为0 endl; } return 0; }3.同一个try语句可以跟上多个catch语句1catch语句可以定义具体处理的异常类型2不同类型的异常由不同的catch语句负责处理类型必须严格匹配不进行任何的类型转换3try语句中可以抛出任何类型的异常4catch(...)用于处理所有类型的异常5任何异常都只能被捕获catch一次#includeiostream #includestring using namespace std; void demo1() { try { throw 1; } catch (char c) { cout catch (char c) endl; } catch (short c) { cout catch (short c) endl; } catch (double c) { cout catch (double c) endl; } catch (int c) { cout catch (int c) endl; } catch (...) { //只能放在最后 } } int main() { demo1(); //catch (int c) return 0; }4.catch语句块中可以抛出异常#includeiostream #includestring using namespace std; int main() { try { throw 0; } catch (int i) { // 专门捕获 int 类型的异常并将抛出的异常值赋值给变量 i。 //程序在执行 throw i 时会直接跳出当前 catch 块不会回头执行同一个 try 后的其他 catch throw i; //i0,执行 throw i,再次抛出 int 类型异常值为 0,但没人处理 } catch (...) { throw; } return 0; }运行结果#includeiostream #includestring using namespace std; int main() { try { try { throw 0; } catch (int i) { cout in:catch (int i) endl; // 专门捕获 int 类型的异常并将抛出的异常值赋值给变量 i。 //程序在执行 throw i 时会直接跳出当前 catch 块不会回头执行同一个 try 后的其他 catch throw i; //i0,执行 throw i,再次抛出 int 类型异常值为 0,但没人处理 } catch (...) { cout in:catch (...) endl; throw; } } catch (...) { cout out:catch (...) endl; } return 0; }运行结果in:catch (int i)out:catch (...)示例#includeiostream #includestring using namespace std; void func(int i) { if (i 0) { throw - 1; } if (i 100) { throw - 2; } if (i 11) { throw - 3; } cout Run func... endl; } void Myfunc(int i) { try { func(i); } catch (int i) { switch (i) { case -1: throw 1; break; case -2: throw 2; break; case -3: throw 3; break; } } } int main() { try { Myfunc(11); } catch (const char* cs) { cout cs endl; } return 0; }运行结果35.异常的类型可以是自定义类型1对于类类型异常的匹配依旧是自上而下严格匹配2一般匹配子类异常的catch放在上部匹配父类异常的catch放在下部#includeiostream #includestring using namespace std; class Base { }; class Exception :public Base { int m_id; //异常id string m_desc; //异常的描述信息 public: Exception(int id, string desc) { m_id id; m_desc desc; } int id()const { return m_id; } string description()const { return m_desc; } }; void func(int i) { if (i 0) { throw - 1; } if (i 100) { throw - 2; } if (i 11) { throw - 3; } cout Run func... endl; } void Myfunc(int i) { try { func(i); } catch (int i) { switch (i) { case -1: throw Exception(-1,1); break; case -2: throw Exception(-2, 2); break; case -3: throw Exception(-3, 3); break; } } } int main() { try { Myfunc(11); } catch (const Exception e) { cout e.id() endl; cout e.description() endl; } return 0; }运行结果-33若在子类的catch分支前加上父类的catch分支#includeiostream #includestring using namespace std; class Base { }; class Exception :public Base { int m_id; //异常id string m_desc; //异常的描述信息 public: Exception(int id, string desc) { m_id id; m_desc desc; } int id()const { return m_id; } string description()const { return m_desc; } }; void func(int i) { if (i 0) { throw - 1; } if (i 100) { throw - 2; } if (i 11) { throw - 3; } cout Run func... endl; } void Myfunc(int i) { try { func(i); } catch (int i) { switch (i) { case -1: throw Exception(-1,1); break; case -2: throw Exception(-2, 2); break; case -3: throw Exception(-3, 3); break; } } } int main() { try { Myfunc(11); } catch (const Base b) { cout catch (const Base b) endl; } catch (const Exception e) { cout e.id() endl; cout e.description() endl; } return 0; }运行结果catch (const Base b)6.C标准库中提供了实用异常类族标准库中的异常都是从exception类派生的exception类有两个主要的分支1logic_error常用于程序中的可避免逻辑错误2runtime_error常用于程序中无法避免的恶性错误
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2421240.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!