欢迎来到三一办公! | 帮助中心 三一办公31ppt.com(应用文档模板下载平台)
三一办公
全部分类
  • 办公文档>
  • PPT模板>
  • 建筑/施工/环境>
  • 毕业设计>
  • 工程图纸>
  • 教育教学>
  • 素材源码>
  • 生活休闲>
  • 临时分类>
  • ImageVerifierCode 换一换
    首页 三一办公 > 资源分类 > PPT文档下载  

    【教学课件】第六章例外处理(Exceptions).ppt

    • 资源ID:5663453       资源大小:347.97KB        全文页数:22页
    • 资源格式: PPT        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    三方登录下载: 微信开放平台登录 QQ登录  
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP免费专享
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    【教学课件】第六章例外处理(Exceptions).ppt

    1,第六章 例外处理(Exceptions),Exceptions的概念例外处理自定义例外,2,1Public class HelloWorld2public static void main(String args)3int i=0;4String greetings=“Hello World!”,”Hello!”,5“HELLO WORLD!”;6while(i4)7System.out.println(greetingsi);8i+;91011,Hello World!Hello!HELLO WORLD!Java.lang.ArrayIndexOutOfBoundsExceptionat 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,示例:ListOfNumbers,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();,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 Try2/code that might throw a partcular exception3 catch(MyExceptionType e)4/code to excute if a MyExceptionType exception is thrown5 catch(Exception e)6/code to execute if a general Exception exception is thrown7 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 size;i+)out.println(Value at:+i+=+victor.elementAt(i);catch(ArrayIndexOutOfBoundsException e)System.err.println(Caught ArrayIndexOutOfBoundsException:+e.getMessage();catch(IOException e)System.err.println(Caught IOException:+e.getMessage();finally if(out!=null)System.out.println(Closing PrintWriter);out.close();else System.out.println(PrintWriter not open);,14,writeList方法中的try语句块的执行可能有三种情况:出现了IOException 出现了数组越界错误 正常退出,Entering try statement Caught IOException:OutFile.txt PrintWriter not open,Entering try statementCaught ArrayIndexOutOfBoundsException:10=10Closing 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;finallySystem.out.println(“This is always printed”);i+;,Hello World!This is always printedHello!This is always printedHELLO!This is always printedRe-setting Index ValueThis 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 Exceptionprivate 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 ServerTimeOutExceptionint success;int portToConnect=80;success=open(serverName,portToConnect);if(success=-1)throw new ServerTimedOutException(“Could not connect”,80);,22,获得例外并处理,Public void findServer()tryconnectMe(defaultServer);catch(ServerTimeOutException e)System.out.println(“Server timed out,try another”);tryconnectMe(alternateServer);catch(ServerTimeOutException e1)System.out.println(“No server avaliable”);,

    注意事项

    本文(【教学课件】第六章例外处理(Exceptions).ppt)为本站会员(小飞机)主动上传,三一办公仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知三一办公(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    备案号:宁ICP备20000045号-2

    经营许可证:宁B2-20210002

    宁公网安备 64010402000987号

    三一办公
    收起
    展开