第七章异常及其处理_第1页
第七章异常及其处理_第2页
第七章异常及其处理_第3页
第七章异常及其处理_第4页
第七章异常及其处理_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第七章 异常及其处理(exception and its handling )7.1 程序运行错误及其处理程序在运行中可能出错,例如在除法运算中用 0 作除数、实数运算中求负数的开方根、在计算机监控系统中所采集的数据越限,等等。当程序出错时,一般要求用户进行处理,如重新输入数值或给出新的控制量等,必要时则中断程序。程序首先应检测到出错,然后才能提示用户进行处理或由程序自动进行处理。传统的错误处理方式唯有依靠函数返回提示错误用的局部标志数据或者全局标志数据来提示出错(全局数据的安全性差,极易受到破坏) 。这类做法的困难是程序只能在函数返回主程序后才能检查标志数据的提示内容。错误处理的一种新的方式是使用“异常” (exception) 。异常处理是由程序设计语言提供的运行时刻错误处理的一种方式。一旦程序出现错误,随即引发和抛出“异常” ,程序能在一处或多处合适的地方自动地捕获异常并及时进行必要的处理。例 1求负数的开方根时传统的错误处理方式:/ sqrt_negative_1.cpp/ error occurs when the square root of a negative number is calculated/ system subroutine used#include #include / ptototype: double sqrt( double );#include / ptototype: void exit(int);double sqroot(double number)if ( number #include / ptototype: double sqrt( double );/#include /不再需要 exit( )double sqroot(double number)if ( number / Definition of function quotient. Demonstrates throwing / an exception when a divide-by-zero exception is encountered.double quotient( double numerator, double denominator )if ( denominator = 0 ) throw denominator;/被抛出的异常对象是预定义类型数据return numerator / denominator;/ Driver programvoid main()/ the try block wraps the code that may throw an / exception and the code that should not execute/ if an exception occurstry std:cout / exception_3.cpp/ same as exception_1.cpp, only differing in / throwing an object of a class instead of a double value#include #include / Definition of function quotient. Demonstrates throwing / an exception when a divide-by-zero exception is encountered.double quotient( double numerator, double denominator )if ( denominator = 0 ) throw exception( );/被抛出的异常对象是异常类即 class exception 的对象,/class exception is declared in the include file return numerator / denominator;/ Driver programvoid main()try std:cout ,在7.3 中将会详细介绍它。catch 程序块中内容将在下一小节7.2.2 中介绍。以上程序中,当出现异常时,抛出异常,程序流即自动退出 try 程序块,进入 catch 程序块,进行处理后即进入 catch 程序块以后的语句,不再继续执行 try 程序块中抛出异常语句以后的其他语句(上例中不再执行第三条语句“quotient ( 4.5, 9 )”) 。以上程序中无法显示异常内容,但只需将以上程序略加修改,即可显示异常内容。如exception_4.cpp(未显示整个程序)中,只需将double quotient( int numerator, int divisor ) 函数的第一句“if ( divisor = 0 ) throw exception( );”改为:“if ( divisor = 0 )throw exception( “Divide-by-zero exception“ ); /使用系统的异常类的对象并初始化”即可!也就是在建立 class exception 的对象时,使用字符串“divide-by-zero exception“将该异常对象的字符串数据初始化,输入用于显示异常性质的字符串。在调用异常对象中的 what( )函数时就能显示该字符串。这就获得如下更明确的运行结果:/* Results:The quotient of 18/9 is 2Divide-by-zero exception */以前提到过,当函数级联调用时,使用显示出错的函数返回值,要逐级向上递送,很不方便。而使用异常处理就方便了。例 3 函数级联调用时的异常处理/ exception_5.cpp/ cascaded invocation of sub-routines#include #include / Definition of function quotient. Demonstrates throwing / an exception when a divide-by-zero exception is encountered.double quotient( double numerator, double denominator )if ( denominator = 0 ) throw exception( “Divide-by-zero exception“ );/使用系统的异常类return numerator / denominator;double h( double numerator, double denominator )double result = quotient( numerator, denominator );std:cout f( ) - g( ) - h( ) -quotient( ),在函数返回时则采取相反路径,即 quotient( ) - h( ) - g( ) - f( ) - main( )。但在出现异常时,也即调用主函数中以下语句cout int arr = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10;int LEN; / number of elementsint /使用系统的异常类并初始化其对象return numerator / denominator;void main()double numerator, denominator;std:cout numerator denominator;try if (!std:cin) throw exception( “Wrong input type exception“ );std:cout #include double denominator;class Except_derive : public exceptionchar *message;public:Except_derive(char * ptr) message = ptr; const char * what( ) const return message; void handling ( ) std:cout denominator;void main()double numerator;bool flag = true;std:cout numerator denominator;while ( flag ) tryif ( denominator = 0 ) throw Except_derive( “divide by zero!“ );std:cout#define MAX_T 28#define MIN_T 16int temperature;double denominator;class exceptchar *message;public:except (char * ptr) message = ptr; const char * what( ) const return message; virtual void handling ( ) std:cout denominator; void action ( ) std:cout MAX_T )std:cout temperature;std:cout numerator denominator;while ( flag ) try if ( temperature MAX_T )|( temperature MAX_T )?( mes_high ):( mes_low );result = quotient ( numerator, denominator );std:cout “The quotient is: “ result std:endl;flag = false;/ The following order should not be reversedcatch ( except_derive ex ) ex.action( ); catch ( except ex ) ex.action( ); /

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论