动手动脑10.28
一、请阅读并运行AboutException.java示例,然后通过后面的几页PPT了解Java中实现异常处理的基础知识。
代码
1 import javax.swing.*; 2 3 class AboutException { 4 public static void main(String[] a) 5 { 6 int i=1, j=0, k; 7 k=i/j; 8 9 10 try 11 { 12 13 k = i/j; // Causes division-by-zero exception 14 //throw new Exception("Hello.Exception!"); 15 } 16 17 catch ( ArithmeticException e) 18 { 19 System.out.println("被0除. "+ e.getMessage()); 20 } 21 22 catch (Exception e) 23 { 24 if (e instanceof ArithmeticException) 25 System.out.println("被0除"); 26 else 27 { 28 System.out.println(e.getMessage()); 29 30 } 31 } 32 33 34 finally 35 { 36 JOptionPane.showConfirmDialog(null,"OK"); 37 } 38 39 } 40 }
把可能会发生错误的代码放进try语句块中。
当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。
catch语句块中的代码用于处理错误。
当异常发生时,程序控制流程由try语句块跳转到catch语句块。
不管是否有异常发生,finally语句块中的语句始终保证被执行。
如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。
二、动手动脑:多层的异常捕获
阅读以下代码(CatchWho.java),写出程序运行结果:
代码
1 public class CatchWho { 2 public static void main(String[] args) { 3 try { 4 try { 5 throw new ArrayIndexOutOfBoundsException(); 6 } 7 catch(ArrayIndexOutOfBoundsException e) { 8 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 9 } 10 11 throw new ArithmeticException(); 12 } 13 catch(ArithmeticException e) { 14 System.out.println("发生ArithmeticException"); 15 } 16 catch(ArrayIndexOutOfBoundsException e) { 17 System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 18 } 19 } 20 }
运行结果:
ArrayIndexOutOfBoundsException/内层try-catch
发生ArithmeticException
三、动手动脑:多层的异常捕获
写出CatchWho2.java程序运行的结果
代码
public class CatchWho2 {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArithmeticException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
}
throw new ArithmeticException();
}
catch(ArithmeticException e) {
System.out.println("发生ArithmeticException");
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
}
}
}
运行结果:
ArrayIndexOutOfBoundsException/外层try-catch
四、动手动脑
请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。
代码
1 public class EmbededFinally { 2 3 4 public static void main(String args[]) { 5 6 int result; 7 8 try { 9 10 System.out.println("in Level 1"); 11 12 13 try { 14 15 System.out.println("in Level 2"); 16 // result=100/0; //Level 2 17 18 try { 19 20 System.out.println("in Level 3"); 21 22 result=100/0; //Level 3 23 24 } 25 26 catch (Exception e) { 27 28 System.out.println("Level 3:" + e.getClass().toString()); 29 30 } 31 32 33 finally { 34 35 System.out.println("In Level 3 finally"); 36 37 } 38 39 40 // result=100/0; //Level 2 41 42 43 } 44 45 catch (Exception e) { 46 47 System.out.println("Level 2:" + e.getClass().toString()); 48 49 } 50 finally { 51 52 System.out.println("In Level 2 finally"); 53 54 } 55 56 // result = 100 / 0; //level 1 57 58 } 59 60 catch (Exception e) { 61 62 System.out.println("Level 1:" + e.getClass().toString()); 63 64 } 65 66 finally { 67 68 . System.out.println("In Level 1 finally"); 69 70 } 71 72 } 73 74 }
运行结果:
in Level 1
in Level 2
in Level 3
Level 3:class java.lang.ArithmeticException
In Level 3 finally
In Level 2 finally
In Level 1 finally
当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。
五、动手动脑
finally语句块一定会执行吗?(请通过 SystemExitAndFinally.java示例程序回答上述问题)
代码
1 public class SystemExitAndFinally { 2 3 4 public static void main(String[] args) 5 { 6 7 try{ 8 9 10 System.out.println("in main"); 11 12 throw new Exception("Exception is thrown in main"); 13 14 //System.exit(0); 15 16 17 } 18 19 catch(Exception e) 20 21 { 22 23 System.out.println(e.getMessage()); 24 25 System.exit(0); 26 27 } 28 29 finally 30 31 { 32 33 System.out.println("in finally"); 34 35 } 36 37 } 38 39 40 }
运行结果:
in main
Exception is thrown in main
finally一定会执行