1、C+程序设计HUAIYIN INSTITUTE OF TECHNOLOGY异常处理主讲教师:陈婷主讲教师:陈婷18.1 异常的概念18.2 异常的实现18.3 异常的规则18.4 默认异常处理18.5 异常派生族系本章内容#include using namespace std;void divided(double a,double b)/实现两个浮点数除法double result=a/b;/会不会有问题?cout“Result=”resultendl;int main()double a,b;coutab;divided(a,b);/调用divided函数return 0;18.1 异常
2、的概念Ch18_01#include#include using namespace std;int main()char*ch;/字符指针long long size;coutsize;coutSize Value:sizeendl;ch=new charsize;/申请堆空间,是否有问题?coutch;cout“String Size=”strlen(ch)endl;delete ch;/释放堆空间return 0;18.1 异常的概念Ch18_02异常(Exception):程序有时会遇到运行阶段错误,导致程序无法正常地运行下去。例如:程序试图打开一个不可用的文件请求过多的内存遭遇不能容
3、忍的值说明:运行异常,可以预料,但不能避免,它是由系统运行环境造成的程序要有能够解决这种运行异常的能力18.1 异常的概念异常处理机制使用异常处理机制的步骤:定义异常。将那些有可能产生错误的语句框定在try语句块中。定义异常处理(捕获异常)。将异常处理的语句放在catch语句块中,以便异常被传递过来时进行处理。抛掷异常。检测是否产生异常,若是,则通过throw语句抛掷异常。18.2 异常的实现 try语句:定义异常 catch语句:捕获异常 throw语句:抛掷异常#include using namespace std;void divided(double a,double b)/实现两个
4、浮点数除法double result;try /定义异常if(b=0.0)throw b;/抛掷异常result=a/b;cout“Result=”resultendl;catch(double val)/捕获异常cout“Exception occurs!Exception value is”valendl;int main()double a,b;coutab;divided(a,b);return 0;18.2 异常的实现Ch18_03说明:try语句块表示块中的语句可能会发生异常,放在其中加以监控。注意:C+只理会受监控的运行异常。throw后面的表达式的类型被称为所引发的异常类型。在
5、try语句块之后必须紧跟一个或多个catch语句,目的是对发生的异常进行处理(注意:try之前不允许出现catch)。catch()括号中的声明只能容纳一个形参,当类型与抛掷异常的类型匹配时,该catch()语句块便称捕获了一个异常而转到其块中进行异常处理。catch()形参的值则为throw语句抛掷的异常值。当异常发生时,try语句块中异常之后的语句不再执行。18.2 异常的实现#include using namespace std;void divided(double a,double b)/实现两个浮点数除法double result;try /定义异常if(b=0.0)throw
6、b;/抛掷异常result=a/b;cout“a/b=”resultendl;result=b/a;/存在除0异常,未被监控cout“b/a=”resultendl;catch(double val)/捕获异常cout“Exception occurs!Exception value is”valendl;int main()double a,b;coutab;divided(a,b);return 0;18.3 异常的规则Ch18_04 C+只理会受监控的运行异常#include using namespace std;int main()int idx;coutidx;try /定义异常i
7、f(idx=0)throw 10;/int型异常else if(idx=1)throw 13.14;/double型异常else if(idx=2)throw c;/char型异常elsethrow“C+”;/字符串异常18.3 异常的规则catch(int n)/捕获int型异常cout“Int Exception:”nendl;catch(double d)/捕获double型异常cout“Double Exception:”dendl;catch(char c)/捕获char型异常cout“Char Exception:”cendl;catch(char*s)/捕获字符串异常cout“S
8、tring Exception:”sendl;return 0;Ch18_05 寻找catch中的最佳匹配来进行异常处理#include using namespace std;int main()int idx;coutidx;try /定义异常if(idx=0)throw 10;/int型异常else if(idx=1)throw 13.14;/double型异常else if(idx=2)throw c;/char型异常elsethrow“C+”;/字符串异常18.3 异常的规则catch(int)/捕获int型异常cout“Int Exception!”endl;catch(doubl
9、e)/捕获double型异常cout“Double Exception!”endl;catch(char)/捕获char型异常cout“Char Exception!”endl;catch(char*)/捕获字符串异常cout“String Exception!”endl;return 0;catch中的形参名可以省略,但类型不能省略#include using namespace std;class CException ;/类定义int main()int idx;coutidx;try /定义异常if(idx=0)throw 10;/int型异常else if(idx=1)throw 1
10、3.14;/double型异常else if(idx=2)throw c;/char型异常else if(idx=3)throw CException();/类异常18.3 异常的规则elsethrow“C+”;/字符串异常catch(int)/捕获int型异常cout“Int Exception!”endl;catch(double)/捕获double型异常cout“Double Exception!”endl;catch(char)/捕获char型异常cout“Char Exception!”endl;catch(CException)/捕获类异常cout“Class Exception!
11、endl;return 0;当无匹配的catch进行异常处理时,系统调用默认异常处理程序abort()来终止程序运行。Ch18_06#include using namespace std;class CException ;/类定义int main()int idx;coutidx;try /定义异常if(idx=0)throw 10u;/unsigned int型异常else if(idx=1)throw 13.14f;/float型异常else if(idx=2)throw c;/char型异常elsethrow CException();/类异常18.3 异常的规则catch(int
12、)/捕获int型异常cout“Int Exception!”endl;catch(double)/捕获double型异常cout“Double Exception!”endl;catch(char)/捕获char型异常cout“Char Exception!”endl;catch(CException)/捕获类异常cout“Class Exception!”endl;return 0;抛掷异常与异常处理程序之间是按数据类型严格匹配来捕获的,不允许类型转换。Ch18_07#include using namespace std;int main()double a,b,result;coutab
13、try /定义异常if(b=0.0)throw b;/抛掷异常result=a/b;catch(double)/捕获、处理异常cout“Exception of dividing zero!Please input b again!”b;result=a/b;catch(int)cout“Int Exception!”endl;/捕获、处理异常cout“a/b=”resultendl;/catch块后的语句return 0;18.3 异常的规则Ch18_08当catch分程序执行完毕时,则跟随最后一个catch分程序的代码就会被执行。#include using namespace std;
14、double divided(double a,double b)/实现两个浮点数除法if(b=0.0)throw b;/抛出异常return a/b;int main()try /定义异常cout“13.14/2.0=”divided(13.14,2.0)endl;cout“13.14/0.0=”divided(13.14,0.0)endl;cout“13.14/3.0=”divided(13.14,3.0)endl;catch(double)/捕获异常cout“Exception of dividing zero occurs!”endl;return 0;18.3 异常的规则可以将抛掷异
15、常与处理异常放在不同的函数中#include using namespace std;class CException ;/类定义int main()int idx;coutidx;try /定义异常if(idx=0)throw 10;/int型异常else if(idx=1)throw 13.14;/double型异常else if(idx=2)throw c;/char型异常else if(idx=3)throw CException();/类异常elsethrow“Exception”;/字符串异常18.4 默认异常处理catch(int)/捕获int型异常cout“Int Except
16、ion!”endl;catch(double)/捕获double型异常cout“Double Exception!”endl;catch(char)/捕获char型异常cout“Char Exception!”endl;catch(CException)/捕获类异常cout“Class Exception!”endl;catch()/捕获所有其他异常cout“Unexpected Exception!”endl;return 0;Ch18_09下列关于异常的叙述错误的是:A.编译错属于异常,可以抛出B.运行错属于异常C.硬件故障也可当异常抛出D.只要是编程者认为是异常的都可当异常抛出下列叙述错
17、误的是:A.throw的操作数表示异常类型B.throw的操作数值可以区别不同的异常C.throw抛出不同异常时需要用不同的操作数类型来区分D.throw语句抛出的异常可以不被捕获下列叙述错误的是:A.catch()语句可捕获所有类型的异常B.一个try语句可以有多个catch语句C.catch()语句可以放在catch语句组的前面D.程序中try语句与catch语句是一个整体,缺一不可下列程序运行结果为:class Apublic:A()coutAn;char fun0()A A1;throw(E);return 0;int main()try coutfun0()n;catch(char c)coutcn;return 0;A.A EB.0 A EC.0 ED.E






