一、运行AboutException.java,解释为什么会出现这样的结果
import javax.swing.*; class AboutException { public static void main(String[] a) { int i=1, j=0, k; k=i/j; try { k = i/j; //Causes division-by-zero exception throw new Exception("Hello.Exception!"); } catch ( ArithmeticException e) { System.out.println("被0除. "+ e.getMessage()); } catch (Exception e) { if (e instanceof ArithmeticException) System.out.println("被0除"); else { System.out.println(e.getMessage()); } } finally { JOptionPane.showConfirmDialog(null,"OK "+k); } } }
(1)运行结果为
(2)分析
在java中Throwable类有两个直接子类:Exception:出现的问题是可以被捕获的;Error:系统错误,通常由JVM处理。可捕获的异常又可以分为两类:(1)Check异常:直接派生自Exception的异常类,必须被捕获或再次声明抛出(2)Runtime异常:派生自RuntimeException的异常类。而AboutException.java中出现的错误就是Runtime出现的异常,
二、解释奇怪的现象
(1) int i=1, j=0, k;
k=i/j;
(2) double d1=100,d2=0,result;
result=d1/d2;
System.out.println("浮点数除以零:" + data);
(1)的代码运行时会引发异常,而(2)的代码不会引发异常!为什么?
·原因分析
因为对于(1)而言,java生成的是idiv字节码指令,而(2)java生成的是ddiv字节码指令,JVM在具体实现这两个指令是,采用了不同的处理策略,导致两段代码运行时得到不同的结果
三、阅读以下代码(CatchWho.java),写出程序运行结果:
public class CatchWho { public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsException 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"); } } }
结果为:
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"); } } }
结果为:
总结:
throw一个异常后,必须catch成功,才能throw下一个异常,系统不能累积处理异常!
四、多个嵌套,请先阅读 EmbedFinally.java示例,再运行它,观察其输出并进行总结。
public class EmbededFinally { public static void main(String args[]) { int result; try { System.out.println("in Level 1"); try { System.out.println("in Level 2"); // result=100/0; //Level 2 try { System.out.println("in Level 3"); result=100/0; //Level 3 } catch (Exception e) { System.out.println("Level 3:" + e.getClass().toString()); } finally { System.out.println("In Level 3 finally"); } // result=100/0; //Level 2 } catch (Exception e) { System.out.println("Level 2:" + e.getClass().toString()); } finally { System.out.println("In Level 2 finally"); } // result = 100 / 0; //level 1 } catch (Exception e) { System.out.println("Level 1:" + e.getClass().toString()); } finally { System.out.println("In Level 1 finally"); } } }
结果为:
总结:当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。
五、finally语句块一定会执行吗?
public class SystemExitAndFinally { public static void main(String[] args) { try{ System.out.println("in main"); throw new Exception("Exception is thrown in main"); //System.exit(0); } catch(Exception e) { System.out.println(e.getMessage()); System.exit(0); } finally { System.out.println("in finally"); } } }
结果为:
不一定,因为当代码 System.exit(0);意思是,终止JAVA虚拟机,导致不能执行finally的内容!
六、课后作业
import java.util.*; public class score { public static void main(String[] args) { Scanner str=new Scanner(System.in); String score; System.out.println("请输入成绩:"); while(true) { try { score=str.next(); if(score.matches("\D*")) throw new InputException(); else { int proo=Integer.parseInt(score); System.out.println("输入正确!"); judge(proo); break; } } catch(InputException e) { System.out.println("请输入数字"); } } str.close(); } public static void judge(int pro) { if(pro>=0&&pro<60) System.out.println("成绩不合格!"); else if(pro<70) System.out.println("成绩合格!"); else if(pro<80) System.out.println("成绩中!"); else if(pro<90) System.out.println("成绩良!"); else if(pro<=100) System.out.println("成绩优!"); else System.out.println("输入成绩只能在1~·100之间"); } } class InputException extends Exception{}
运行结果