- 异常的基本语法
- C++异常的处理关键字
- try throw catch
- 可以出现异常的代码 放到 try块
- 利用throw抛出异常
- 利用catch捕获异常
- catch( 类型) 如果想捕获其他类型 catch(…)
- 如果捕获到的异常不想处理,而继续向上抛出,利用 throw
- 异常必须有函数进行处理,如果都不去处理,程序自动调用 terminate函数,中断掉
- 异常可以是自定义数据类型
- C++异常的处理关键字
- 栈解旋
- 从try代码块开始,到throw抛出异常之前,所有栈上的数据都会被释放掉,
- 释放的顺序和创建顺序相反的,这个过程我们称为栈解旋
- 异常的接口声明
- 在函数中 如果限定抛出异常的类型,可以用异常的接口声明
- 语法: void func()throw(int ,double)
- throw(空)代表 不允许抛出异常
- 异常变量的生命周期
- //抛出的是 throw MyException(); catch (MyException e) 调用拷贝构造函数 效率低
- //抛出的是 throw MyException(); catch (MyException &e) 只调用默认构造函数 效率高 推荐
- //抛出的是 throw &MyException(); catch (MyException *e) 对象会提前释放掉,不能在非法操作
- //抛出的是 new MyException(); catch (MyException *e) 只调用默认构造函数 自己要管理释放
- 异常的多态使用
- 提供基类异常类
- class BaseException
- 纯虚函数 virtual void printError() = 0;
- 子类空指针异常 和 越界异常 继承 BaseException
- 重写virtual void printError()
- 测试 利用父类引用指向子类对象
- 提供基类异常类
- 系统标准异常
- 引入头文件 #include <stdexcept>
- 抛出越界异常 throw out_of_range(“…”)
- 获取错误信息 catch( exception & e ) e.what();
实例一
#include<iostream>
#include<string>
#include<stdexcept>
using namespace std;
class person {
private:
string name;
int age;
public:
person() {
cout << "person" << endl;
}
person(string name,int age):name(name),age(age) {
if (age < 0 || age>150) {
throw out_of_range("年龄有问题! ");
}
}
virtual void printerror() {
cout << "person print error" << endl;
};
~person() {
cout << "~person" << endl;
}
};
class son:public person{
public:
son() {
cout << "son" << endl;
}
void printerror() {
cout << "son error" << endl;
}
~son() {
cout << "~son" << endl;
}
};
void fun()throw(int) {
throw 1;
}
void test() {
try {
//throw 1;
//throw person();
//throw "abc";
//string str = "1234567890";
//throw str;
//fun();
//throw son();
person p("21",999);
}
catch(int){
cout << "int error" << endl;
}
catch (char) {
cout << "char error" << endl;
}
catch (double) {
cout << "double error" << endl;
throw;
//默认double
}
catch (string) {
cout << "string error" << endl;
}
//catch (person p) {
// cout << "person error" << endl;
//}
catch (person &p) {
cout << "person error" << endl;
p.printerror();
}
catch (exception& e) {
cout << e.what() << endl;
}
catch (...) {
cout << "other error" << endl;
throw ;
}
}
int main() {
try {
test();
}
catch (...) {
cout << "other error" << endl;
}
system("pause");
return EXIT_SUCCESS;
}
实例 二
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include<stdexcept>//2013Vs可以不用
#include<string>
class MyOut_of :public exception
{
public:
MyOut_of(const char *errorinfo)
{
//const char*转换string
this->m_Info = string(errorinfo);
}
MyOut_of(const string errorinfo)
{
this->m_Info = errorinfo;
}
const char * what() const
{
//把string转换const char*
return this->m_Info.c_str();
}
public:
string m_Info;
};
class Maker
{
public:
Maker(int age)
{
if (age<0 || age>150)
{
//throw out_of_range("年龄不在范围内");
throw MyOut_of("自己的异常类,年龄不在范围内");
}
else
{
this->age = age;
}
}
public:
int age;
};
void test()
{
try
{
Maker m(200);
}
catch (out_of_range &ex)
{
cout << ex.what() << endl;
}
catch (MyOut_of &ex)
{
cout << ex.what() << endl;
}
}
int main()
{
test();
system("pause");
return EXIT_SUCCESS;
}