天津科技大学Java异常处理_第1页
天津科技大学Java异常处理_第2页
天津科技大学Java异常处理_第3页
天津科技大学Java异常处理_第4页
天津科技大学Java异常处理_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

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

文档简介

1第六章异常处理

异常(Exception)概念异常处理方法自定义异常类天津科技大学Java异常处理第1页21 publicclassHelloWorld{2 publicstaticvoidmain(Stringargs[]){3 inti=0;4 Stringgreetings[]={“HelloWorld!”,”Hello!”,5 “HELLOWORLD!!”};6 while(i<4){7 System.out.println(greetings[i]);8 i++;9 }10 }11 }HelloWorld!Hello!HELLOWORLD!Java.lang.ArrayIndexOutOfBoundsException atHelloWorld.main(HelloWorld.java:7)天津科技大学Java异常处理第2页3异常概念异常在程序运行时打断正常程序流程任何不正常情况。异常情况试图打开文件不存在网络链接中止操作符越界要加载类文件不存在Java中定义了各种异常天津科技大学Java异常处理第3页4ThrowableErrorExceptionVirtualMachineErrorAWTErrorRuntimeExceptionIOExceptionEOFExceptionFileNotFoundExceptionArithmeticExceptionNullPointerExceptionIndexOutOfBoundsExceptionJava中定义异常天津科技大学Java异常处理第4页5Java处理异常Error极难恢复严重错误,普通不由程序处理。RuntimeException程序设计或实现上问题,如数组越界、除零、空引用等。其它异常通常是由环境原因引发,而且能够被处理。如文件不存在,无效URL等。天津科技大学Java异常处理第5页6Java异常类体系中常见异常ArithmeticException:除0错造成异常NullPointerException:当对象没有实例化时,就试图经过该对象变量访问其数据或方法。ArrayIndexOutOfBoundsException:数组越界异常IOException:输入/输出时可能产生各种异常SecurityException:由浏览器中负责安全控制SecurityManager类抛出天津科技大学Java异常处理第6页76.2异常处理异常处理指程序取得异常并处理,然后继续程序执行。为了使程序安全,Java要求假如程序中调用方法有可能产生某种类型异常,那么调用该方法程序必须采取对应动作处理异常。处理异常方式方式一:捕捉并处理异常方式二:将方法中产生异常抛出天津科技大学Java异常处理第7页8捕捉与处理异常•try语句块•catch语句块•finally语句块天津科技大学Java异常处理第8页9try语句块普通形式:

try{ Javastatements//一条或多条可能产生例外java语句。

}try语句后必须跟随最少一个catch或finally语句块。天津科技大学Java异常处理第9页10catch语句块catch语句块提供异常处理普通格式:catch(SomeThrowableObjectvariableName){Javastatements}•SomeThrowableObject:能够被处理异常类名,必须是Throwable类子类•variableName:是异常处理程序中能够引用代表被捕捉异常变量名称。•Javastatements:当捕捉到异常时执行Java语句。天津科技大学Java异常处理第10页11finally语句块finally语句块不论是否发生异常都要执行。普通用于关闭文件或释放其它系统资源。天津科技大学Java异常处理第11页12捕捉与处理异常示例publicstaticvoidmain(Stringargs[]){ inti=0; Stringgreetings[]={“HelloWorld!”,”Hello!”,”HELLO!”}; while(i<4){

try{ System.out.println(greetings[i]); }catch(ArrayIndexOutOfBoundsExceptione){ System.out.println(“Re-settingIndexValue”); i=-1; }finally{ System.out.println(“Thisisalwaysprinted”); } i++; }}HelloWorld!ThisisalwaysprintedHello!ThisisalwaysprintedHELLO!ThisisalwaysprintedRe-settingIndexValueThisisalwaysprinted天津科技大学Java异常处理第12页13各种异常同时处理天津科技大学Java异常处理第13页14异常处理能够针对这个体系中任意一个类。叶结点详细、专用异常处理;中间结点通用异常处理。能够处理该结点及其子类类型异常。例:writeList方法:try{...}catch(Exceptione){System.err.println("Exceptioncaught:"+e.getMessage());}天津科技大学Java异常处理第14页15异常处理——try,catch和finally语句1try{2//codethatmightthrowapartcularexception3}catch(MyExceptionTypee){4//codetoexcuteifaMyExceptionTypeexceptionisthrown5}catch(Exceptione){6//codetoexecuteifageneralExceptionexceptionisthrown7}finally{}天津科技大学Java异常处理第15页16示例(P.165,6-2:ListOfNumbers.javaimportjava.io.*;importjava.util.Vector;publicclassListOfNumbers{privateArrayListlist;privatestaticfinalintsize=10;publicListOfNumbers(){ list=newArrayList(size);for(inti=0;i<size;i++)list.add(newInteger(i));}publicvoidwriteList(){PrintWriterout=newPrintWriter(newFileWriter("OutFile.txt"));for(inti=0;i<size;i++)out.println(“Valueat:”+i+“=”+list.get(i));out.close();}}天津科技大学Java异常处理第16页17publicvoidwriteList(){PrintWriterout=null;

try{System.out.println("Enteringtrystatement");out=newPrintWriter(newFileWriter("OutFile.txt"));for(inti=0;i<size;i++)out.println("Valueat:"+i+"="+list.get(i));}catch(ArrayIndexOutOfBoundsExceptione){System.err.println("CaughtArrayIndexOutOfBoundsException:"+ e.getMessage());}catch(IOExceptione){System.err.println("CaughtIOException:"+e.getMessage());}finally{if(out!=null){System.out.println("ClosingPrintWriter");out.close();}else{System.out.println("PrintWriternotopen");}}}天津科技大学Java异常处理第17页18writeList方法中try语句块执行可能有三种情况:出现了IOException

出现了数组越界错误

正常退出

EnteringtrystatementCaughtIOException:OutFile.txtPrintWriternotopenEnteringtrystatementCaughtArrayIndexOutOfBoundsException:10>=10ClosingPrintWriterEnteringtrystatementClosingPrintWriter天津科技大学Java异常处理第18页19异常处理——抛出异常可能产生异常方法表明将不处理该异常,而该异常将被抛到调用该方法程序。例:publicvoidtroublesome()throwsIOException{ ….. }假如一个异常在返回到main()时还未被处理,则程序将非正常终止。天津科技大学Java异常处理第19页20例:publicObjectpop()throwsEmptyStackException{Objectobj;if(size==0)

thrownewEmptyStackException();obj=objectAt(size-1);setObjectAt(size-1,null);size--;returnobj;}抛出异常throw语句:throwsomeThrowableObject;异常处理——抛出异常天津科技大学Java异常处理第20页怎样修改例子6-2?21天津科技大学Java异常处理第21页226.3自定义异常类定义异常类,是Exception类子类,可包含普通类内容。publicclassServerTimeOutExceptionextendsException{ privateStringreason; privateintport; publicServerTimeOutException(Stringreason,intport){ this.reason=reason; this.port=port; } publicStringgetReason(){ returnreason; } publicintgetPort(){ returnport; }}天津科技大学

温馨提示

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

评论

0/150

提交评论