1.
实验源代码
package yichanglia; import javax.swing.*; class AboutException { public static void main(String[] a) { int i=1, j=0, k; 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"); } } }
2.
实验源代码
package yichanglib; 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"); } } }
实验内容结果截图
实验执行结果为捕捉到了内层的异常,在内层的try 抛出异常之后,紧接着catch进行捕捉,然后在执行外部的try之后,进行下一个的catch的执行,而外部的catch没有try语句的作用,不执行最外部的catch语句
3.
实验源代码:
package yichanglic; 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"); } } }
执行结果截图:
该试验的结果是抛出外部的异常,因为抛出的一场名称不同,所以捕捉到了外部的异常
4.
实验源代码
package yichangEmbededFinally; public class EmbededFinally { public static void main(String args[]) { int result; try { System.out.println("in Level 1"); //result=100/0; 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"); } } }
实验执行结果
在try语句的各种调用时,根据result=100/0的位置的不同而进行抛出的异常不同,实例中的result=100/0的位置在执行玩第三个try之后进行书写,被catch到System.out.println("Level 2:" + e.getClass().toString());当有多层嵌套的finally时,异常在不同的层次抛出 ,在不同的位置抛出,可能会导致不同的finally语句块执行顺序。
5.
实验源代码
package yichangSystemExitAndFinally; 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"); } } }
JVM是java虚拟机,finally是由JVM保证执行,而System.exit(0)是正常退出程序,结束JVM的运行,那么最后finally就不再执行。finally语句不被执行的唯一情况是先执行了用于终止程序的System.exit()方法
6.
// UsingExceptions.java // Demonstrating the getMessage and printStackTrace // methods inherited into all exception classes. public class PrintExceptionStack { public static void main( String args[] ) { try { method1(); } catch ( Exception e ) { System.err.println( e.getMessage() + " " ); e.printStackTrace(); } } public static void method1() throws Exception { method2(); } public static void method2() throws Exception { method3(); } public static void method3() throws Exception { throw new Exception( "Exception thrown in method3" ); } }
实验执行结果截图
7.总结
(1):如果try{}中产生的异常在catch中没有处理,系统将停止程序,也不会执行finally中的语句。基本过程是用try语句块包住要监视的语句,如果在try语句块内出现异常,则异常会被抛出,你的代码在catch语句块中可以捕获到这个异常并做处理;还有以部分系统生成的异常在Java运行时自动抛出。你也可以通过throws关键字在方法上声明该方法要抛出异常,然后在方法内部通过throw抛出异常对象。finally语句块会在方法执行return之前执行。
(2):在java语言中,通常将可能出现异常的语句放入try{}语句中,将出现错误后需要执行的语句放入到catch{}语句中,将无论是否发生异常都要执行的语句放在finally{}语句中,使系统跟家完美的进行
(3)如果系统出现系统错误或者运行Runtime异常,jvm会结束程序运行,不一定会执行finally{}中的语句
8.
package yichangjxuexijibie; import java.util.*; class AException extends Exception { String mrg; AException() { mrg="输入有误"; } public String toString() { return mrg; } } public class Test { public static void main(String args[]) { while(1>0) { Scanner sc = new Scanner(System.in ); System.out.println("请输入考试成绩(0~100):"); try { String s = sc.nextLine(); getnum(s); } catch (AException e) { System.out.println(e.toString()); } } } private static void getnum(String s) throws AException { for (int i = s.length()-1; i >= 0;i--) { int chr = s.charAt(i); if (chr < 48 || chr > 57) { throw new AException(); } } double num = Double.parseDouble(s); if (num < 0 || num> 100) { throw new AException(); } if (num>= 0 && num< 60) { System.out.print("不及格 "); } else if (num >= 60 && num <= 70) { System.out.print("及格 "); } else if (num>= 70 && num<= 80) { System.out.print("中 "); } else if (num >= 80 && num <= 90) { System.out.print("良 "); } else { System.out.print("优 "); } } }