题目一:请阅读并运行AboutException.java示例
AboutException.java
1 import javax.swing.JOptionPane; 2 3 public class AboutException { 4 public static void main(String[] args) { 5 int i=1,j=0,k; 6 k=i/j; 7 8 try { 9 k=i/j;//Causes division-by-zero exception 10 //throw new Exception("Hello.Exception!"); 11 }catch(ArithmeticException e) { 12 System.out.println("被0除."+e.getMessage()); 13 }catch(Exception e) { 14 if(e instanceof ArithmeticException) { 15 System.out.println("被0除"); 16 }else { 17 System.out.println(e.getMessage()); 18 } 19 }finally { 20 JOptionPane.showConfirmDialog(null, "OK"); 21 } 22 } 23 }
运行结果:
题目二:阅读以下代码(CatchWho.java)
CatchWho.java
1 public class CatchWho { 2 public static void main(String[] args) { 3 try{ 4 try{ 5 throw new ArrayIndexOutOfBoundsException(); 6 }catch(ArrayIndexOutOfBoundsException e) { 7 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 8 } 9 throw new ArithmeticException(); 10 }catch(ArithmeticException e){ 11 System.out.println("发生ArithmeticException"); 12 }catch(ArrayIndexOutOfBoundsException e){ 13 System.out.println("ArrayIndexOutOfBoundsException" + "/外层try-catch"); 14 } 15 } 16 }
执行结果:
CatchWho2.java
1 public class CatchWho2 { 2 public static void main(String[] args){ 3 try{ 4 try{ 5 throw new ArrayIndexOutOfBoundsException(); 6 }catch(ArithmeticException e) { 7 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 8 } 9 throw new ArithmeticException(); 10 } catch(ArithmeticException e) { 11 System.out.println("发生ArithmeticException"); 12 }catch(ArrayIndexOutOfBoundsException e){ 13 System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 14 } 15 } 16 }
执行结果:
题目三:请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。
EmbedFinally.java
1 public class EmbededFinally { 2 public static void main(String[] args) { 3 int result; 4 try { 5 System.out.println("in Level 1"); 6 try { 7 System.out.println("in Level 2"); 8 //result=100/0;//Level 2 9 try { 10 System.out.println("in Level 3"); 11 result=100/0;//Level 12 }catch(Exception e) { 13 System.out.println("Level 3:"+e.getClass().toString()); 14 }finally { 15 System.out.println("In Level 3 finally"); 16 } 17 //result=100/0;//Level 2 18 }catch(Exception e) { 19 System.out.println("Level 2:"+e.getClass().toString()); 20 }finally { 21 System.out.println("In Level 2 finally"); 22 } 23 //result=100/0;//Level 1 24 }catch(Exception e) { 25 System.out.println("Level 1:"+e.getClass().toString()); 26 }finally { 27 System.out.println("In Level 1 finally"); 28 } 29 } 30 }
执行结果为:
将result的表达式换在第二个try里面,其执行代码为:
1 public class EmbededFinally { 2 public static void main(String[] args) { 3 int result; 4 try { 5 System.out.println("in Level 1"); 6 try { 7 System.out.println("in Level 2"); 8 result=100/0;//Level 2 9 try { 10 System.out.println("in Level 3"); 11 //result=100/0;//Level 12 }catch(Exception e) { 13 System.out.println("Level 3:"+e.getClass().toString()); 14 }finally { 15 System.out.println("In Level 3 finally"); 16 } 17 //result=100/0;//Level 2 18 }catch(Exception e) { 19 System.out.println("Level 2:"+e.getClass().toString()); 20 }finally { 21 System.out.println("In Level 2 finally"); 22 } 23 //result=100/0;//Level 1 24 }catch(Exception e) { 25 System.out.println("Level 1:"+e.getClass().toString()); 26 }finally { 27 System.out.println("In Level 1 finally"); 28 } 29 } 30 }
执行结果为:
将result的表达式换在第一个try里面,其执行代码为:
1 public class EmbededFinally { 2 public static void main(String[] args) { 3 int result; 4 try { 5 System.out.println("in Level 1"); 6 try { 7 System.out.println("in Level 2"); 8 //result=100/0;//Level 2 9 try { 10 System.out.println("in Level 3"); 11 //result=100/0;//Level 12 }catch(Exception e) { 13 System.out.println("Level 3:"+e.getClass().toString()); 14 }finally { 15 System.out.println("In Level 3 finally"); 16 } 17 //result=100/0;//Level 2 18 }catch(Exception e) { 19 System.out.println("Level 2:"+e.getClass().toString()); 20 }finally { 21 System.out.println("In Level 2 finally"); 22 } 23 result=100/0;//Level 1 24 }catch(Exception e) { 25 System.out.println("Level 1:"+e.getClass().toString()); 26 }finally { 27 System.out.println("In Level 1 finally"); 28 } 29 } 30 }
执行结果为:
总结:
在try..catch...finally语句中,try里面的是率先执行的,若是try内的语句有错误,catch中的语句才会执行,若是无错误,便会执行finally内的语句。
题目四:辨析:finally语句块一定会执行吗? 请通过 SystemExitAndFinally.java示例程序回答上述问题
SystemExitAndFinally.java
1 public class SystemExitAndFinally { 2 public static void main(String[] args){ 3 try{ 4 System.out.println("in main"); 5 throw new Exception("Exception is thrown in main"); 6 //System.exit(0); 7 }catch(Exception e){ 8 System.out.println(e.getMessage()); 9 System.exit(0); 10 }finally{ 11 System.out.println("in finally"); 12 } 13 } 14 }
执行结果为:
若程序改为:
则会出现以下错误:
结论:finally代码块不一定会被执行