异常:存在于运行时的反常行为,这些行为超过了函数的正常的功能范围。ios
异常处理:处理程序中的错误,异常处理机制为程序中异常检测和异常处理这两部分的协做提供支持。c++
在C++中,异常处理包括:ide
#include<iostream> using namespace std; //异常基本语法 int divide(int x ,int y){ if (y == 0){ throw y; //抛异常 } return x / y; } void test01(){ //试着去捕获异常 try{ divide(10, 0); } catch (int e){ //异常时根据类型进行匹配 cout << "除数为" << e << "!" << endl; } } void CallDivide(int x,int y){ divide(x, y); } //a() -> b() - >c() -> d(),d()中的异常一层层向上抛到terminate的标准库函数,直处处理为止 void test02(){ try{ CallDivide(10,0); } catch (int e){ cout << "除数为" << e << endl; } } //C++异常机制跨函数 //异常必须处理,若是异常抛到顶层尚未处理,程序便会挂掉。 int main(){ //test01(); test02(); }
异常被抛出后,从进入try块起,到异常被抛前,这期间在栈上构造的全部对象,都会被自动析构,析构的顺序与构造的顺序相反,这一过程即为栈解旋。函数
构造函数没有返回类型,没法经过返回值来报告运行状态,因此经过异常机制来解决构造函数的出错问题。this
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class Person{ public: Person(){ cout << "对象构建!" << endl; } ~Person(){ cout << "对象析构!" << endl; } }; int divide(int x,int y){ Person p1, p2; if (y == 0){ throw y; } return x / y; } void test01(){ try{ divide(10,0);//栈解旋 } catch (int e){ cout << "异常捕获!" << endl; } } int main(void) { test01(); return 0; } /* 结果: 对象构建! 对象构建! 对象析构! 对象析构! 异常捕获! */
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; //这个函数只能抛出int float char三种类型异常,抛出其余的就报错 void func() throw(int,float,char){ throw "abc"; } //不能抛出任何异常 void func02() throw(){ throw -1; } //能够抛出任何类型异常 void func03(){ } int main(void) { try{ func(); } catch (char* str){ cout << str << endl; } catch (int e){ cout << "异常!" << endl; } catch (...){ //捕获全部异常 cout << "未知类型异常!" << endl; } return 0; } //结果: 未知类型异常!
throw的异常是有类型的,能够是数字、字符串、类对象,catch需严格匹配异常类型。spa
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; void func01(){ throw 1; //抛出int类型异常 } void func02(){ throw "exception"; } class MyException{ public: MyException(const char* str){ error = new char[strlen(str)+1]; strcpy(error, str); } MyException(const MyException& ex){ this->error = new char[strlen(ex.error) + 1]; strcpy(this->error,ex.error); } MyException& operator=(const MyException& ex){ if (this->error != NULL){ delete[] this->error; this->error = NULL; } this->error = new char[strlen(ex.error) + 1]; strcpy(this->error, ex.error); } void what(){ cout << error << endl; } ~MyException(){ if (error != NULL){ delete[] error; } } public: char* error; }; void fun03(){ throw MyException("我刚写异常!"); } void test01(){ try{ func01(); } catch (int e){ cout << "int 异常捕获!" << endl; } //---------------------------------- try{ func02(); } catch (const char* e){ cout << "const char* 异常捕获!" << endl; } //---------------------------------- try{ fun03(); } catch (MyException e){ e.what(); } } int main(void){ test01(); return 0; } /* int 异常捕获! const char* 异常捕获! 我刚写异常! */
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class MyException { public: MyException() { cout << "构造函数!" << endl; } MyException(const MyException& ex) { cout << "拷贝构造!" << endl; } ~MyException() { cout << "析构函数!" << endl; } }; void func() { //throw &(MyException()); //建立匿名对象,调用构造 //throw new MyException();//用指针接 throw MyException(); } void test01(); int main(void) { test01(); return 0; } /* void test01();{ try { func(); } catch (MyException e) { cout << "异常捕获!" << endl; } } 普通类型去接,结果为: 构造函数! 拷贝构造! 异常捕获! 析构函数! 析构函数! */ /* void test01();{ try { func(); } catch (MyException& e) { cout << "异常捕获!" << endl; } } 引用去接,结果为: 构造函数! 异常捕获! 析构函数! */ /* void test01();{ try { func(); } catch (MyException* e) { cout << "异常捕获!" << endl; detele e; } } 指针去接,结果为: 构造函数! 异常捕获! 析构函数! */