1.Aboutexception程序
代码:
import javax.swing.*; class AboutException { public static void main(String[] a) { int i=1, j=0, k; //infinity正无穷 // 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"); } } }
运行结果:
修改后(删除出现错误的语句)
Finally必定运行。
2.若将1中int类型改为double,输出结果为infinity而非异常,因为javac在编译时将int语句生成为idiv字节码指令,而double则生成ddiv字节码指令,JVM在具体实现这两个指令时,采用了不同的处理策略,导致两段代码运行时得到不同的结果。
3.阅读代码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"); } } }
运行结果:
4.运行catchwho2并写出结果:
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"); } } }
运行结果:
5.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语句块执行顺序。
6.通过SystemExitAndFinally.java示例程序验证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"); } } }
运行结果截图:
由此,finally语句块一定会执行。
7.编写一个程序,此程序在运行时要求用户输入一个整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
代码;
package boke1125; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class Grade { public static void main(String[] args) { while(true){ //判断整数//判断成绩等级 try { BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); //抛出受控的异常 System.out.println("请输入考试成绩:(正整数)"); int grade = Integer.parseInt(buf.readLine()); //有可能引发运行时异常 System.out.println("成绩为: "+grade); if(grade<0||grade>100){ MyException1 e1=new MyException1("成绩应在0~100之间!"); throw e1; } if(grade>=0&&grade<60){ System.out.println("你处于不及格水平"); break; } else if(grade>=60&&grade<70){ System.out.println("你处于及格水平"); break; } else if(grade>=70&&grade<80){ System.out.println("你处于中等水平"); break; } else if(grade>=80&&grade<90){ System.out.println("你处于良好水平"); break; } else if(grade>=90&&grade<=100){ System.out.println("你处于优秀水平"); break; } } //以下异常处理语句块是必须的,否则无法通过编译 catch(MyException1 e1) { System.out.println(e1); } catch(IOException e) { System.out.println("I/O错误"); } //以下异常处理语句块可以省略,不影响编译,但在运行时出错 catch(NumberFormatException e) { System.out.println("成绩必须为整数"); } } } } class MyException1 extends Exception{ public MyException1(String str){ super(str); } }
运行结果截图: