收藏 分销(赏)

通过异常处理错误.ppt

上传人:pc****0 文档编号:14018031 上传时间:2026-05-28 格式:PPT 页数:23 大小:75KB 下载积分:10 金币
下载 相关 举报
通过异常处理错误.ppt_第1页
第1页 / 共23页
通过异常处理错误.ppt_第2页
第2页 / 共23页


点击查看更多>>
资源描述
*,单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,*,第九章 通过异常处理错误,1,Exceptions,的概念,例外处理,自定义例外,2,1Public class,HelloWorld,2public static void main(String,args,),3,int,i=0;,4String greetings=“Hello World!”,”Hello!”,5“HELLO WORLD!”;,6while(i4),7,System.out.println(greetingsi,);,8i+;,9,10,11,Hello World!,Hello!,HELLO WORLD!,Java.lang.ArrayIndexOutOfBoundsException,at HelloWorld.main(HelloWorld.java:7),3,Exception,的概念,Exception,是在程序运行时打断正常程序流程的,异常的情况,试图打开的文件不存在,网络链接中断,操作符越界,要加载类文件不存在,Java,中定义了各种例外,4,Java,中定义了各种例外。,Java.lang.Throwable,是这些类的父类。,Throwable,Error,Exception,VirtualMachineError,AWTError,RuntimeException,IOException,EOFException,FileNotFoundException,ArithmeticException,NullPointerException,IndexOutOfBoundsException,Java,中定义的例外,5,Error,很难恢复的严重错误,一般不由程序处理。,RuntimeException,程序设计或实现上的问题,如数组越界等。,其它例外,通常是由环境因素引起的,并且可以被处理的。,如文件不存在,无效,URL,等。,6,例外处理,扑获并处理例外,将方法中产生的例外抛出,7,import,java.io,.*;,import java.util.Vector;,public class,ListOfNumbers,private Vector victor;,private static final,int,size=10;,public,ListOfNumbers,(),victor=new Vector(size);,for(,int,i=0;i size;i+),victor.addElement(new,Integer(i);,public void,writeList,(),printWriter,out=new,PrintWriter(new,FileWriter(OutFile.txt,),);,for(,int,i=0;i size;i+),out.println(Value,at:+i+=+,victor.elementAt(i,),);,out.close();,示例:,ListOfNumbers,8,捕获与处理例外,Try,语句块,catch,语句块,finally,语句块,9,Try,语句块,一般形式:,try,Java statements/,一条或多条可能产生例,外的,java,语句。,try,语句后必须跟随至少一个,catch,或,finally,语句块。,10,Catch,语句块,Catch,语句块提供错误处理。,一般格式:,catch(,SomeThrowableObject,variableName,),Java statements,SomeThrowableObject,:能够被处理的例外类名,,必须是,throwable,类的子类,variableName,:,是例外处理程序中能够引用的代表,被捕获例外的变量名称。,Java statements:,当捕获到例外时执行的,java,语句。,11,Finally,语句块,将先前方法的状态清除,并可以将控制转移到程,序的其他地方。,finally,语句块无论是否发生异常都要执行。,12,例外处理,Try ,catch,和,finally,语句,1,Try,2 /code that might throw a,partcular,exception,3 ,catch,(MyExceptionType,e),4 /code to,excute,if a,MyExceptionType,exception is thrown,5 ,catch,(Exception e),6 /code to execute if a general Exception exception is thrown,7 ,finally,13,public void,writeList,(),PrintWriter,out=null;,try,System.out.println(Entering,try statement);,out=new,PrintWriter,(new,FileWriter(OutFile.txt,);,for(,int,i=0;i=10,Closing,PrintWriter,Entering try statement,Closing,PrintWriter,15,多种例外的同时处理,16,例外处理可以针对这个体系中的任意一个类。,叶结点:是具体、专用的例外处理;,中间结点:是通用的例外处理。可以处理该结点及其子类类型的例外。,例:,writeList,方法:,try,.,catch(Exception e),System.err.println(Exception,caught:+,e.getMessage,();,17,捕,获与处理例外示例,Public static void main(String,args,),int,i=0;,String greetings=“Hello World!”,”Hello!”,”HELLO!”;,while(i4),try,System.out.println(greetingsi,);,catch,(ArrayIndexOutOfBoundsException,e),System.out.println(“Re,-setting Index Value”);,i=-1;,finally,System.out.println(“This,is always printed”);,i+;,Hello World!,This is always printed,Hello!,This is always printed,HELLO!,This is always printed,Re-setting Index Value,This is always printed,18,例外处理,抛出例外,可能产生例外的方法表明将不处理该例外,而该例外将被抛到调用该方法的程序。,例:,public void troublesome()throws,IOException,.,如果一个例外在返回到,main(),时还未被处理,则程序将非正常终止。,19,例外处理,抛出例外,例:,public Object pop(),throws,EmptyStackException,Object,obj,;,if(size=0),throw new,EmptyStackException,();,obj,=,objectAt(size,-1);,setObjectAt(size,-1,null);,size-;,return,obj,;,抛出例外的,throw,语句:,throw,someThrowableObject,20,自定义例外,定义例外类,是,Exception,类的子类,可包含普通类的内容。,public class,ServerTimeOutException,extends Exception,private String reason;,private,int,port;,public,ServerTimeOutException(String,reason,int,port),this.reason=reason;,this.port=port;,public String,getReason,(),return reason;,public,int,getPort,(),return port;,21,抛出产生的例外,Public void,connectMe(String,serverName,),throws,ServerTimeOutException,int,success;,int,portToConnect,=80;,success=,open(serverName,portToConnect,);,if(success=-1),throw new,ServerTimedOutException,(,“Could not connect”,80);,22,获得例外并处理,Public void,findServer,(),try,connectMe(defaultServer,);,catch,(ServerTimeOutException,e),System.out.println(“Server,timed out,try another”);,try,connectMe(alternateServer,);,catch,(ServerTimeOutException,e1),System.out.println(“No,server,avaliable,”);,23,
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服