c sharp language

上传人:真** 文档编号:243155144 上传时间:2024-09-17 格式:PPT 页数:23 大小:120.50KB
返回 下载 相关 举报
c sharp language_第1页
第1页 / 共23页
c sharp language_第2页
第2页 / 共23页
c sharp language_第3页
第3页 / 共23页
点击查看更多>>
资源描述
,*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,异常处理,异常(,Exception,)是运行时产生的错误。使用,C#,的异常处理子系统,我们能够以标准化可控制的方式来处理运行时错误。,异常处理可以说是对不正常情况的处理,包括发生错误时的处理,异常处理通过允许程序定义代码块来简化错误处理,此代码块称为异常处理程序,出现错误时自动执行它。,System.Exception,类,在,C#,中,异常用类来表示,所有异常类都必须从内部异常类,Exception,派生而来的,而,Exception,是,System,名字空间的一部分。因此所有异常都是,Exception,的子类。,在,C#,中有两类常规异常:,C#,运行时系统(,CLR,)产生的异常(,SystemException,),以及应用程序产生的异常(,ApplicationException,)。,System.OutOfMemoryException,当试图通过,new,来分配内存而失败时抛出。,System.IndexOutOfRangeException,当试图通过一个比零小或者超出数组边界的标签来索,引一个数组时抛出。,DivideByZeroException,当试图用整数类型数据除以零时抛出。,System.NullReferenceException,试图对空引用进行操作,也就是说引用没有指向对象时抛出。,OverflowException,.,当一个算术操作溢出时抛出。,C#,中定义的常用标准异常都是从,SystemException,派生而来的,例如:,异常 怎样被处理,C#,中异常处理由,4,个关键字来管理:,try,、,catch,、,throw,和,finally,。,工作方式如下,:要监视是否产生异常的程序语句包含在,try,模块中。如果,try,模块内产生异常,那么系统自动抛出此异常,同时也可以手动用(,throw,)抛出异常,然后使用,catch,捕捉此异常,并以合理的方式处理它。,异常处理语法,try ,/ suspect code,catch ,/ handle exceptions,/ Demonstrate exception handling.,using System;,class ExcDemo1 ,public static void Main() ,int,nums,= new int4;,try ,Console.WriteLine(Before,exception is generated.);,/ Generate an index out-of-bounds exception.,for(int,i=0; i 10; i+) ,numsi, = i;,Console.WriteLine(nums0: 1, i,numsi,);,Console.WriteLine(this,wont be displayed);,catch (,IndexOutOfRangeException,) ,/ catch the exception,Console.WriteLine(Index,out-of-bounds!);,Console.WriteLine(After,catch statement.);,输出结果:,Before exception is generated.,nums0: 0,nums1: 1,nums2: 2,nums3: 3,Index out-of-bounds!,After catch statement.,请注意,,catch,子句中不指定参数,只有需要访问异常对象时才需要参数。在某些情况下,异常处理程序能够使用异常对象的值来获得此错误的附加信息。,Message-,获取描述当前异常的消息,.,Source,获取或设置导致错误的应用程序或对象的名称。,ToString,()-,创建并返回当前异常的字符串表示形式,using System;,class,ExceptionTest,public static void,Main(string,args,), int a=30;,int b=0;,int c = 0;,try,c=a/b;,catch (DivideByZeroException e),Console.WriteLine(e.Message,= +,e.Message,);,Console.WriteLine(e.Source,= +,e.Source,);,Console.WriteLine(e.ToString,()= +,e.ToString,();,return;,Console.WriteLine(a,=0 b=1 a/b=2,a,b,c,);,输出结果:,e.Message,=,试图除以零。,e.Source,= ConsoleApplication8,e.ToString,()=,System.DivideByZeroException,:,试图除以零。,at,ExceptionTest.Main(String,args,) in c:documents and settings,whtc,my documentsvisual studio projectsconsoleapplication8consoleapplication8class1.cs:,line 11,异常允许合理处理错误,异常处理的关键优点之一是:它允许程序响应错误并继续运行。例如:下列数组的一个元素除以另一个元素。如果出现除数为,0,的情况,那么产生,DivideByZeroException,异常。在此程序中,此异常的处理是报告错误消息并继续执行。,using System;,class ExcDemo3 ,public static void Main() ,int numer = 4, 8, 16, 32, 64, 128 ;,int denom = 2, 0, 4, 4, 0, 8 ;,for(int,i=0; i ,numer.Length,; i+) ,try ,Console.WriteLine(numeri + / + denomi + is + numeri/denomi);,catch (DivideByZeroException) ,/ catch the exception,Console.WriteLine(Cant,divide by Zero!);,输出结果:,4 / 2 is 2,Cant divide by Zero!,16 / 4 is 4,32 / 4 is 8,Cant divide by Zero!,128 / 8 is 16,请注意,:异常被处理后就从系统删除。,使用多个,catch,语句,可以有多个语句与同一个,try,模块相关联。每一个,catch,语句必须捕捉不同类型的异常,异常处理的顺序是至关重要的,例如:,Sytem.DivideByZeroException,派生自,System.ArthmeticException,。,如果试图在捕获,DivideByZeroException,之前捕获,ArthmeticException,,那么程序将永远捕获不到,DivideByZeroException,。因为,DivideByZeroException,是,ArthmeticException,类型,也就是说它派生自,ArthmeticException,。,/ Use multiple catch statements.,using System;,class ExcDemo4 ,public static void Main() ,/ Here, numer is longer than denom.,int numer = 4, 8, 16, 32, 64, 128, 256, 512 ;,int denom = 2, 0, 4, 4, 0, 8 ;,for(int i=0; i numer.Length; i+) ,try ,Console.WriteLine(numeri + / + denomi + is + numeri/denomi);,catch (DivideByZeroException) ,/ catch the exception,Console.WriteLine(Cant divide by Zero!);,catch (,IndexOutOfRangeException,) ,/ catch the exception,Console.WriteLine(No,matching element found.);,输出结果:,4 / 2 is 2,Cant divide by Zero!,16 / 4 is 4,32 / 4 is 8,Cant divide by Zero!,128 / 8 is 16,No matching element found.,No matching element found.,捕捉所有异常,有时我们想要捕捉所有异常,而不管其类型,此时使用没有参数的,catch,语句。,using System;,class ExcDemo5 ,public static void Main() ,int numer = 4, 8, 16, 32, 64, 128, 256, 512 ;,int denom = 2, 0, 4, 4, 0, 8 ;,for(int,i=0; i ,numer.Length,; i+) ,try ,Console.WriteLine(numeri + / + denomi + ,is+numeri,/denomi);,catch ,Console.WriteLine(Some,exception occurred.);, , ,输出结果如下:,4 / 2 is 2,Some exception occurred.,16 / 4 is 4,32 / 4 is 8,Some exception occurred.,128 / 8 is 16,Some exception occurred.,Some exception occurred.,throw,抛出异常,前面的示例捕捉,C#,自动产生的异常。但是通过使用,throw,语句能够手动抛出(产生)异常。,using System;,class,ThrowDemo,public static void Main() ,try ,Console.WriteLine(Before,throw.);,throw new DivideByZeroException();,catch (DivideByZeroException) ,/ catch the exception,Console.WriteLine(Exception,caught.);,Console.WriteLine(After,try/catch block.);, ,输出结果:,Before throw.,Exception caught.,After try/catch block.,重新抛出异常,同一个,catch,语句捕捉的异常能够重新抛出,所以外层,catch,能够捕捉它。重新抛出异常,可以允许多个处理程序访问此异常。,using System;,class,Rethrow,public static void,genException,(),int numer = 4, 8, 16, 32, 64, 128, 256, 512 ;,int, denom = 2, 0, 4, 4, 0, 8 ;,for(int,i=0; i,numer.Length,; i+),try, Console.WriteLine(numeri + / +,denomi + is + numeri/denomi);,catch (DivideByZeroException),Console.WriteLine(Cant,divide by Zero!);,catch (,IndexOutOfRangeException,),Console.WriteLine(No,matching element found.);,throw; /,rethrow,the exception, ,class,RethrowDemo,public static void Main(),try,Rethrow.genException,(); ,catch(IndexOutOfRangeException,),Console.WriteLine(Fatal,error - + ,programterminated,.);, ,输出结果如下:,4 / 2 is 2,Cant divide by Zero!,16 / 4 is 4,32 / 4 is 8,Cant divide by Zero!,128 / 8 is 16,No matching element found.,Fatal error - program terminated.,请记住:重新抛出一个异常时,不会由同一个,catch,语句来捕捉它,它将传播到下一个,catch,语句。,finally,语句,有时我们想要定义离开,try/catch,模块时必须执行的代码块,可以用,finally,语句。,try ,/ suspect code,catch ,/ handle exceptions,finally ,/ always do this,try,模块不论是正常结束还是产生异常而结束,最后执行的代码是,fnally,模块中定义的代码。,示例六:,using System;,class Finally,public static void,Main(string,args,),int a,30;,int,b=0;,int,c = 0;,try,c=a/b; ,catch (DivideByZeroException e),Console.WriteLine,(,除以,0,所产生的异常,: +,e.Message,);,return;,finally,Console.WriteLine(a,=0 b=1 c=2,a,b,c,); ,输出结果:,除以,0,所产生的异常,:,试图除以零。,a=30 b=0 c=0,自定义异常类,创建异常的过程很简单,只要定义一个从,ApplicationException,派生的类即可。,using System;,public class,MyException,:,ApplicationException,private static string,myMessage,=“,自定义异常类,MyException,;,public,MyException,() :,base(myMessage,) ,public,MyException(string,msg,) :,base(msg,) ,class,MyDivide,public static void,Main(string,args,), int a=40;,int b=0;,int c = 0;,if (b=0),try,throw new,MyException,();,catch (,MyException,e),Console.WriteLine,(,除以,0,所产生的异常,:+,e.Message,);,return;,else c=a/b;,Console.WriteLine(a,=0 b=1 a/b=2,a,b,c,);,输出结果如下:,除以,0,所产生的异常,:,自定义异常类,MyException,
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 图纸专区 > 小学资料


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!