C++异常处理基本语法:
代码如下:
#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){cout << "除数为0!" << endl;} */catch (int e){cout << "除数为" << e << "!" << endl;}
}int main()
{test01();return 0;
}
- C++异常机制跨函数
- C++异常必须处理
代码如下:
#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){cout << "除数为0!" << endl;} */catch (int e){cout << "除数为" << e << "!" << endl;}
}void callDivide(int x, int y)
{divide(x, y);
}void test02()
{try {callDivide(10,0);}catch (int e){cout << "除数为" << e << "!" << endl;}
}int main()
{/*test01();*/test02();return 0;
}
栈解旋
异常被抛出后,从进入try块起,这期间在栈上构造的所有对象,都会被自动析构。析构的顺序与构造的顺序相反,这一过程称为栈的解旋。
代码如下:
#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){cout << "除数为0!" << endl;} */catch (int e){cout << "异常捕获"<< endl;}
}void callDivide(int x, int y)
{divide(x, y);
}int main()
{test01();return 0;
}
测试结果:
异常接口声明:
由于C++编译器,忽略C++异常规范,所有在VS可以编译通过运行,但是在linux是不行的。
代码如下:
#include <iostream>using namespace std;//这个函数只能抛出int,float,char三种类型异常,抛出其他的就报错
void func() throw(int ,float,char)
{throw "abc";
}//不能抛出任何异常
void func02() throw()
{throw - 1;
}//可以抛出任何异常
void func03()
{throw - 1;
}int main()
{try {func();}catch (char * str){cout << str << endl;}catch (int e){cout << "异常" << endl;}catch (...){cout << "未知异常" << endl;}return 0;
}
C标准异常类使用举例和编写自己的异常类:
代码如下:
#include <iostream>
#include <stdexcept>
using namespace std;class Person {
public:Person(){mAge = 0;}void setAge(int age){if (age < 0 || age > 100){throw out_of_range("年龄应该在0-100之间!");}this->mAge = age;}public:int mAge;
};void test01()
{Person p;try {p.setAge(1000);}/*catch (out_of_range e){cout << e.what() << endl;}*/catch (exception e){cout << e.what() << endl;}}class MyOutOfRange :public exception
{
public:MyOutOfRange(const char *error){pError = new char[strlen(error) + 1];strcpy(pError, error);}~MyOutOfRange(){if (pError != nullptr){delete[] pError;}}virtual const char *what() const{return pError;}public:char *pError;
};void fun02()
{throw MyOutOfRange("我自己的out_of_range!");}void test02()
{try{fun02();}catch (exception &e){cout << e.what() << endl;}
}int main()
{/*test01();*/test02();return 0;
}
继承在异常中的应用:
代码如下:
#include <iostream>
using namespace std;//异常基类
class BaseMyException
{
public:virtual void what() = 0;virtual ~BaseMyException(){}
};class TargetSpaceNullException :public BaseMyException
{
public:virtual void what(){cout << "目标空间空!" << endl;}~TargetSpaceNullException() {}
};class SourceSpaceNullException :public BaseMyException
{
public:virtual void what(){cout << "原空间为空!" << endl;}~SourceSpaceNullException(){}
};void copy_str(char *taget, const char *source)
{if (taget == nullptr){throw TargetSpaceNullException();}if (source == nullptr){throw SourceSpaceNullException();}int len = strlen(source) + 1;while (*source != '\0'){*taget = *source;taget++;source++;}
}int main()
{const char *source = "abcdefg";char buf[1024] = { 0 };try {copy_str(buf, source);}catch (BaseMyException &ex){ex.what();}cout << buf << endl;return 0;
}