七、异常处理
1. 异常的概念
C++的异常是指程序在运行期间出现的问题,编译可以通过,说明代码出现了逻辑问题,而不是语法问题。当程序运行的过程中出现了C++标准库中预定义的异常现象时,程序会抛出一个异常对象,此对象需要正确地处理,否则会导致运行终止。
#include <iostream>
using namespace std;
int main()
{
string s = "ahjgds";
// 抛出异常对象,但程序未正确处理
cout << s.at(100) << endl;
cout << "主函数执行结束" << endl;
return 0;
}
![](https://img-blog.csdnimg.cn/img_convert/b1e0818e539aabcb9f6ca8f16310d033.png)
处理异常的方式有两种:
● 抛出异常 throw
● 捕获异常 try-catch
2. 抛出异常
使用throw可以抛出一个任意类型的对象,此对象作为当前程序运行错误的代表。
#include <iostream>
using namespace std;
double division(double a,double b)
{
return a/b;
}
int main()
{
cout << division(1,0) << endl; // inf:infinity无限
cout << "主函数执行结束" << endl;
return 0;
}
上面的代码可以更改为,每当除数等于0时,程序抛出异常,不正确处理则终止。异常对象的抛出,随着函数调用到上一级。
#include <iostream>
using namespace std;
double division(double a,double b)
{
if(b == 0)
// 抛出异常
throw "除数不能为0,请检查代码!";
return a/b;
}
int main()
{
cout << division(1,0) << endl;
cout << "主函数执行结束" << endl;
return 0;
}
![](https://img-blog.csdnimg.cn/img_convert/0a068f3a17b4258a601525fc36b7e28f.png)
3. 自定义异常类型
在本章第二节抛出了一个const char*类型的异常对象,实际上这种异常类型并不属于标准异常类型家族,C++内置了一些标准异常类型,如下所示。
![](https://img-blog.csdnimg.cn/img_convert/eafae6afd269ab53a6ac3ea0fe185119.png)
![](https://img-blog.csdnimg.cn/img_convert/1a0ef0ad4ef63f01a678f60e49164b2d.png)
可以自己写一个异常类继承标准异常类型,从而加入标准异常家族,使用标准异常类型需要引入头文件<stdexcept>
#include <iostream>
// 头文件
#include <stdexcept>
using namespace std;
/**
* @brief The ZeroException class
* 自定义异常类
*/
class ZeroException:public exception
{
public:
// 函数覆盖,what函数只要用于返回异常信息
// throw()是异常规格说明,()中没有任何类型
// 表示what函数不能抛出任何异常
const char* what() const throw()
{
return "除数不能为0,请检查代码!";
}
};
double division(double a,double b)
{
if(b == 0)
// 抛出自定义异常对象
throw ZeroException();
return a/b;
}
int main()
{
cout << division(1,0) << endl;
cout << "主函数执行结束" << endl;
return 0;
}
![](https://img-blog.csdnimg.cn/img_convert/d629384e16bfbe805e79983355240081.png)
4. 捕获异常
4.1 基本使用
使用try-catch代码块进行异常的捕获,try代码块表示尝试执行可能出现异常的代码,catch块表示如果try块中出现了异常,则与catch块捕获的异常类型进行匹配,如果匹配成功,则进入catch块,如果匹配失败,程序仍然终止。
![](https://img-blog.csdnimg.cn/img_convert/963df0b9c68506778b2f9e0eba6637ae.png)
#include <iostream>
// 头文件
#include <stdexcept>
using namespace std;
/**
* @brief The ZeroException class
* 自定义异常类
*/
class ZeroException:public exception
{
public:
// 函数覆盖,what函数只要用于返回异常信息
// throw()是异常规格说明,()中没有任何类型
// 表示what函数不能抛出任何异常
const char* what() const throw()
{
return "除数不能为0,请检查代码!";
}
};
double division(double a,double b)
{
if(b == 0)
// 抛出自定义异常对象
throw ZeroException();
return a/b;
}
int main()
{
try // 尝试执行
{
cout << division(1,0) << endl;
cout << "这句话在异常之后" << endl; // 无法执行
}catch(const ZeroException& e) // 类型匹配捕获
{
cout << e.what() << endl;
// 弥补措施
cout << 1 << endl;
}
try
{
cout << "不会出现异常" << endl;
}catch(const length_error& e)
{
cout << e.what() << endl;
}
string s = "fdfd";
try
{
cout << s.at(100) << endl;
}catch(const overflow_error& e) // 捕获失败,还是会终止运行
{
cout << e.what() << endl;
}
cout << "主函数执行结束" << endl;
return 0;
}
4.2 拓展捕获方式
为了提高异常捕获的概率,可以使用两种手段:
● 多重捕获
● 捕获基类类型
4.2.1 多重捕获
可以同时捕获多个异常类型,当try出现异常时,或按照编写的catch顺序,逐个异常类型进行匹配,匹配成功的catch快会得到执行,匹配失败的catch块不会执行。如果所有的catch块都匹配失败,则程序运行终止。
#include <iostream>
// 头文件
#include <stdexcept>
using namespace std;
/**
* @brief The ZeroException class
* 自定义异常类
*/
class ZeroException:public exception
{
public:
// 函数覆盖,what函数只要用于返回异常信息
// throw()是异常规格说明,()中没有任何类型
// 表示what函数不能抛出任何异常
const char* what() const throw()
{
return "除数不能为0,请检查代码!";
}
};
double division(double a,double b)
{
if(b == 0)
// 抛出自定义异常对象
throw ZeroException();
return a/b;
}
int main()
{
try
{
cout << division(1,0) << endl;
}catch(const invalid_argument& e)
{
cout << "a" << e.what() << endl;
}catch(const underflow_error& e)
{
cout << "b" << e.what() << endl;
}catch(const ZeroException& e)
{
cout << "c" << e.what() << endl;
}
cout << "主函数执行结束" << endl;
return 0;
}
4.2.2 捕获基类异常类型
捕获基类异常类型可以扩大捕获的范围,因为基类异常类型可以匹配所有的派生类异常类型,但是不能精准的判断出现的异常。
#include <iostream>
// 头文件
#include <stdexcept>
using namespace std;
/**
* @brief The ZeroException class
* 自定义异常类
*/
class ZeroException:public exception
{
public:
// 函数覆盖,what函数只要用于返回异常信息
// throw()是异常规格说明,()中没有任何类型
// 表示what函数不能抛出任何异常
const char* what() const throw()
{
return "除数不能为0,请检查代码!";
}
};
double division(double a,double b)
{
if(b == 0)
// 抛出自定义异常对象
throw ZeroException();
return a/b;
}
int main()
{
try
{
cout << division(1,0) << endl;
}catch(const exception& e)
{
cout << e.what() << endl;
}
string s = "fsjkgds";
try
{
cout << s.at(-1) << endl;
}catch(const exception& e) // 匹配任意标准异常类型
{
cout << e.what() << endl;
}
try
{
cout << s.at(-1) << endl;
}catch(...)
{
cout << "可以匹配任意类型" << endl;
}
cout << "主函数执行结束" << endl;
return 0;
}
4.2.3 综合使用
可以把4.2.1与4.2.2节中的方式结合使用,只需要注意基类异常的匹配一定要在派生类异常的匹配之后进行。
#include <iostream>
// 头文件
#include <stdexcept>
using namespace std;
int main()
{
string s = "fsjkgds";
try
{
throw "非标准异常"; // 仅用于示例
cout << s.at(-1) << endl;
}catch(const out_of_range& e)
{
cout << "a" << e.what() << endl;
}catch(const logic_error& e)
{
cout << "b" << e.what() << endl;
}catch(const exception& e)
{
cout << "c" << e.what() << endl;
}catch(...)
{
cout << "d" << endl;
}
cout << "主函数执行结束" << endl;
return 0;
}