Java Programming in Java Exceptions JAVA zxmzjut edu

  • Slides: 33
Download presentation
Java程序设计 Programming in Java 第六章 异常处理(Exceptions) 赵小敏 浙江 业大学软件学院 JAVA zxm@zjut. edu. cn 1

Java程序设计 Programming in Java 第六章 异常处理(Exceptions) 赵小敏 浙江 业大学软件学院 JAVA zxm@zjut. edu. cn 1

先看一个例子 1 2 3 4 5 6 7 8 9 10 11 public class

先看一个例子 1 2 3 4 5 6 7 8 9 10 11 public class Hello. World{ public static void main(String args[ ]){ int i=0; String greetings[ ]={ “Hello World!”, ”Hello!”, “HELLO WORLD!!”}; while ( i<4){ System. out. println(greetings[i]); i++; } } Hello World! } Hello! HELLO WORLD! Java. lang. Array. Index. Out. Of. Bounds. Exception at Hello. World. main(Hello. World. java: 7) 3

Java中定义的异常类 Java中定义了各种异常类。Java. lang. Throwable 是这些类的父类。 Virtual. Machine. Error Throwable AWTError Runtime. Exception Arithmetic. Exception

Java中定义的异常类 Java中定义了各种异常类。Java. lang. Throwable 是这些类的父类。 Virtual. Machine. Error Throwable AWTError Runtime. Exception Arithmetic. Exception Null. Pointer. Exception Index. Out. Of. Bounds. Exception EOFException IOException File. Not. Found. Exception 5

p 159例6 -2:List. Of. Numbers 1. import java. io. *; 2. import java. util.

p 159例6 -2:List. Of. Numbers 1. import java. io. *; 2. import java. util. *; 3. class List. Of. Numbers { 4. private Array. List list; 5. private static final int size = 10; 6. public List. Of. Numbers () { 7. list = new Array. List(size); 8. for (int i = 0; i < size; i++) 9. list. add(new Integer(i)); 10. } 11. public void write. List() { 12. Print. Writer out = new Print. Writer(new File. Writer("Out. File. txt")); 13. for (int i = 0; i < size; i++) 14. out. println("Value at: " + i + " = " + list. get(i)); 15. out. close(); 16. } 17. } 8

例外处理——try , catch和finally 语句 1 Try{ 2 // code that might throw a partcular

例外处理——try , catch和finally 语句 1 Try{ 2 // code that might throw a partcular exception 3 }catch(My. Exception. Type e){ 4 // code to excute if a My. Exception. Type exception is thrown 5 }catch (Exception e){ 6 // code to execute if a general Exception exception is thrown 7 }finally{ } 13

public void write. List() { Print. Writer out = null; 修改例6 -2得write. List()方法,见例6 -3

public void write. List() { Print. Writer out = null; 修改例6 -2得write. List()方法,见例6 -3 try { System. out. println("Entering try statement"); out = new Print. Writer(new File. Writer("Out. File. txt")); for (int i = 0; i <size; i++){ out. println("Value at: " + i + " = " + list. get(i)); } } catch (Array. Index. Out. Of. Bounds. Exception e) { System. err. println("Caught Array. Index. Out. Of. Bounds. Exception: " + e. get. Message()); } catch (IOException e) { System. err. println("Caught IOException: " + e. get. Message()); } finally { if (out != null) { System. out. println("Closing Print. Writer"); out. close(); } else { System. out. println("Print. Writer not open"); } } 14

write. List方法中的try语句块的执行可能有三种情况: • 出现了IOException Entering try statement Caught IOException: Out. File. txt Print. Writer

write. List方法中的try语句块的执行可能有三种情况: • 出现了IOException Entering try statement Caught IOException: Out. File. txt Print. Writer not open • 出现了数组越界错误 Entering try statement Caught Array. Index. Out. Of. Bounds. Exception: 10 >= 10 Closing Print. Writer • 正常退出 Entering try statement Closing Print. Writer 15

捕获与处理异常示例 Public static void main(String args[]){ int i = 0 ; String greetings[]={“Hello World!”,

捕获与处理异常示例 Public static void main(String args[]){ int i = 0 ; String greetings[]={“Hello World!”, ”Hello!”, ”HELLO!”}; while (i<4){ try { Hello World! System. out. println(greetings[i]); This is always printed }catch(Array. Index. Out. Of. Bounds. Exception e){ Hello! System. out. println(“Re-setting Index Value”); This is always printed i=-1; }finally{ HELLO! System. out. println(“This is always printed”); This is always printed Re-setting Index Value } i++; This is always printed } } 18

异常处理——抛出异常 例: public Object pop() throws Empty. Stack. Exception { Object obj; if (size

异常处理——抛出异常 例: public Object pop() throws Empty. Stack. Exception { Object obj; if (size == 0) throw new Empty. Stack. Exception(); obj = object. At(size - 1); set. Object. At(size - 1, null); size--; return obj; } z 抛出异常的throw语句: throw some. Throwable. Object 20

异常可用方法的例子 //: Exception. Methods. java // Demonstrating the Exception Methods public class Exception. Methods

异常可用方法的例子 //: Exception. Methods. java // Demonstrating the Exception Methods public class Exception. Methods { public static void main(String[] args) { try { throw new Exception("Here's my Exception"); } catch(Exception e) { System. out. println("Caught Exception"); System. out. println("e. get. Message(): " + e. get. Message()); System. out. println("e. to. String(): " + e. to. String()); System. out. println("e. print. Stack. Trace(): "); e. print. Stack. Trace(); } } } 22

自定义异常类, 是Exception类的子类, 可包含普通类的内容。 public class Server. Time. Out. Exception extends Exception{ private String reason;

自定义异常类, 是Exception类的子类, 可包含普通类的内容。 public class Server. Time. Out. Exception extends Exception{ private String reason; private int port ; public Server. Time. Out. Exception(String reason, int port){ this. reason = reason; this. port = port; } public String get. Reason(){ return reason; } public int get. Port(){ return port; } 23 }

抛出产生的异常 public void connect. Me(String server. Name) throws Server. Time. Out. Exception{ int success

抛出产生的异常 public void connect. Me(String server. Name) throws Server. Time. Out. Exception{ int success ; int port. To. Connect = 80; success = open(server. Name, port. To. Connect); if(success= -1){ throw new Server. Timed. Out. Exception( “Could not connect”, 80); } } 24

获得异常并处理 public void find. Server(){ … try{ connect. Me(default. Server); }catch(Server. Time. Out. Exception

获得异常并处理 public void find. Server(){ … try{ connect. Me(default. Server); }catch(Server. Time. Out. Exception e){ System. out. println(“Server timed out, try another”); try{ connect. Me(alternate. Server); }catch(Server. Time. Out. Exception e 1){ System. out. println(“No server avaliable”); } } 25

Java异常处理程序的基本形式 try //执行时程序块 catch Exception. Type 1 e //对Exception. Type 1的处理 catch Exception. Type

Java异常处理程序的基本形式 try //执行时程序块 catch Exception. Type 1 e //对Exception. Type 1的处理 catch Exception. Type 2 e //对Exception. Type 2的处理 throw e //再抛出这个“异常” finally try程序块和catch语句 28

1. public class Using. Exceptions { 2. public static void main( String args[] ){

1. public class Using. Exceptions { 2. public static void main( String args[] ){ 3. try{ 4. method 1(); // 1 5. }catch(Exception e){ 6. System. err. println(e. get. Message()); // 2 7. } 8. finally{ 9. System. out. println("Program is end!"); // 3 10. } 11. } 12. public static void method 1() throws Exception { 13. method 2(); //4 14. } 15. public static void method 2() throws Exception { 16. method 3(); //5 17. } 18. public static void method 3() throws Exception { 19. throw new Exception( "Exception thrown in method 3" ); //6 20. } 21. } 29

1. class Test. Exception{ 2. public static void main(String []args){ 3. try{ 4. mb_method();

1. class Test. Exception{ 2. public static void main(String []args){ 3. try{ 4. mb_method(); 5. }catch(Exception e){ 6. System. out. print('m'); 7. } 8. System. out. println('n'); 9. } 10. 11. static void mb_create. Exception(){ 12. throw new Array. Index. Out. Of. Bounds. Exception(); 13. } 14. 15. static void mb_method(){ 16. try{ 17. mb_create. Exception(); 18. System. out. print('a'); 19. }catch(Arithmetic. Exception e){ catch(Array. Index. Out. Of. Bounds. Exception e){ 20. 21. System. out. print('b'); 22. } 23. finally{ 24. System. out. print('c'); 25. } 26. System. out. print('d'); 27. } 28. } 30