✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
📚专栏地址:C/C++知识点
📣专栏定位:整理一下 C++ 相关的知识点,供大家学习参考~
❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪
🎏唠叨唠叨:在这个专栏里我会整理一些琐碎的 C++ 知识点,方便大家作为字典查询~
异常处理
概述
C++中的异常处理是处理运行时错误的过程。 执行异常处理,以便即使在运行时出现错误后也能保持正常的应用程序流转。
在C++中,异常是在运行时抛出的事件或对象。 所有异常都派生自std::exception类。 它是一个可以处理的运行时错误。 如果我们不处理异常,它就会打印异常消息并终止程序。
异常提供了一种转移程序控制权的方式。C++ 异常处理涉及到三个关键字:try、catch、throw。
- try: try 块中的代码标识将被激活的特定异常。它后面通常跟着一个或多个 catch 块。
- catch: 在您想要处理问题的地方,通过异常处理程序捕获异常。catch 关键字用于捕获异常。
- throw: 当问题出现时,程序会抛出一个异常。这是通过使用 throw 关键字来完成的。
标准异常
C++ 提供了一系列标准的异常,定义在 中,可以使用这些标准的异常。它们是以父子类层次结构组织起来的。
try/catch语句
try 块中放置可能抛出异常的代码,try 块中的代码被称为保护代码(有可能出错的代码)。如果有一个块抛出一个异常,捕获异常的方法会使用 try 和 catch 关键字。使用 try/catch 语句的语法如下所示:
try{
// 保护代码(有可能出错的代码)
}catch( ExceptionName e1 )
{
// catch 块
}catch( ExceptionName e2 )
{
// catch 块
}catch( ExceptionName eN ){
// catch 块
}
try 块在不同的情境下会抛出不同的异常,可以尝试罗列多个 catch 语句,用于捕获不同类型的异常。
案例
#include <iostream>
using namespace std;
double div(double x, double y) {
return (x / y);
}
double add(double x, double y) {
return x + y;
}
int main() {
double v1 = 50,v2 = 0;
double result1 = div(v1,v2);
cout << result1 << endl;
double result2 = add(v1, v2);
cout << result2 << endl;
return 0;
}
inf :infinity (linux) 等同于 #INF:infinity (windows)
nan :not a number 等同于 #IND:indeterminate (windows)
inf一般是因为得到的数值,超出浮点数的表示范围(溢出,即阶码部分超过其能表示的最大值);而nan一般是因为对浮点数进行了未定义的操作,如对-1开方。
抛出异常
使用 throw 语句在代码块中的任何地方抛出异常。
throw 语句的操作数可以是任意的表达式,表达式的结果的类型决定了抛出的异常的类型。
以下是尝试除以零时抛出异常的实例:
double div(double a, double b){
if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}
当在div函数里面抛出异常后,程序还是无法正常运行结束。
只抛出,不捕获,程序是无法正常运行的结束的。
捕获异常
catch 块跟在 try 块后面,用于捕获异常。可以指定想要捕捉的异常类型,这是由 catch 关键字后的括号内的异常声明决定的。
当给div函数加上try catch之后,程序可以正常运行结束了。
try {
double result1 = div(v1, v2);
cout << result1 << endl;
}
catch (...) {
cout << "出错了!" << endl;
}
打印刚才throw关键字抛出的异常信息:
try {
double result1 = div(v1, v2);
cout << result1 << endl;
}
catch (const char * msg) {
cerr << msg << endl;
}
自定义异常
通过继承和重载 exception 类来定义新的异常。
#include <iostream>
#include <exception>
using namespace std;
class MyException : public exception {
public:
const char* what() const throw()
{
return "my test exception";
}
};
int add(int v1, int v2) {
if (v1 > 0) {
//如果v1 > 0 ,那么抛出自定义异常myex
MyException myex;
throw myex;
}
return v1 + v2;
}
int main() {
int num1 = 100, num2 = 200;
try {
cout << add(num1, num2) << endl;
}
catch (exception& e) {
cout << e.what() << endl;
}
return 0;
}
what()是一个由 exception 类提供的公共方法。 它用于返回异常的原因。
案例
创建一个数组,创建一个函数取数组元素,当数组下标小于0,或者数组下标大于最大下标时抛出异常,其他正常取值。
- 下标小于0 underflow
- 下标大于n-1 overflow
#include<iostream>
using namespace std;
enum index{underflow, overflow};//under下标小于0,over下标超过最大下标
int array_index(int *arr, int n, int index);
int main()
{
int *arr = new int[10];
for(int i=0; i<10; i++)
A[i] = i;
try
{
cout<<array_index(arr,10,5)<<endl;
cout<<array_index(arr,10,-1)<<endl;
cout<<array_index(arr,10,15)<<endl;
}
catch(index e)
{
if(e == underflow)
{
cout<<"index underflow!"<<endl;
exit(-1);
}
if(e == overflow)
{
cout<<"index overflow!"<<endl;
exit(-1);
}
}
return 0;
}
int array_index(int *arr, int n, int index)
{
if(index < 0) throw underflow;
if(index > n-1) throw overflow;
return arr[index];
}
问题
(1)什么叫异常处理?
答:异常处理是一种程序定义的错误,它对程序的逻辑错误进行设防,并对运行异常加以控制。在C++中,异常处理是对所能预料的运行错误进行处理的一套实现机制。
(2)C++的异常处理机制有何优点?
答:C++的异常处理机制使得异常的引发和处理不必在同一函数中。C++异常处理的目的是在异常发生时,尽可能地减小破坏,周密地善后,而不影响其他部分程序的运行。这样底层的函数可以着重解决具体问题,而不必过多地考虑对异常的处理。上层调用者可以在适当的位置设计对不同类型异常的处理,这在大型程序中是非常必要的