【教学课件】第六章例外处理(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”);,