JavaSE系列第二篇——java异常机制
2017-03-17 00:00
281 查看
请自行运行代码分析,深夜,累,分析可能有误;后续会做详细分析;
package com.mjq.exceptiontest; import javax.management.RuntimeErrorException; /** * 对异常处理机制进行测试 * @author Administrator * java异常机制: * 1.代码执行顺序:无异常:try块=====》finally块======》finally块后 * 抛出异常:try块=====》catch块=====》finally块======》finally块后 * 2.return 检查:无异常:finally块====》try块=====》finally块后 * 抛出异常:finally块=====》catch块====》 * * * * 后续还会修改,可能分析有误;可自行分析。 * * */ public class ExceptionTest { /*====================================================================================================================== * 下面通过代码测试 * 1.JAVA异常捕获机制 * * 2.JAVA try catch finally 及finally块后的代码执行请情况 * * */ /** * 下面的代码不会抛出任何异常 * 运行结果:0 */ public static void test0() { int i = 0; System.out.println(i/1); } /** * 下面的代码有运行时异常抛出 * 但是运行时异常是不受检查的异常,编译器无法检查; * 运行结果: * Exception in thread "main" java.lang.ArithmeticException: / by zero *at com.mjq.exceptiontest.ExceptionTest.test1(ExceptionTest.java:39) *at com.mjq.exceptiontest.ExceptionTest.main(ExceptionTest.java:21) */ public static void test1() { int i = 0; System.out.println(i/i); } /** * try块对可能抛出异常的代码块进行监视,catch块对try块抛出的异常进行捕获 * 机制:虚拟机会对try块中的代码进行监视,如果其中抛出异常,则该异常会被组装成异常对象,首先对try后的catch块由上到下依次进行类型匹配,如果有类型与当前异常对象相匹配的catch块,则运行该catch块中的代码; * 如果当前方法中没有与当前异常类型相匹配的catch块,则该异常对象沿着方法的调用栈中方法调用的地方依次向上抛出,并对每个方法调用处的catch块能够捕获的异常类型与当前异常类型进行匹配,如果成功匹配,则执行该catch块中的代码; * 如果直到main方法依旧没有找到能够与当前异常相匹配的catch块,则虚拟机退出; * 运行结果:ArithmeticException * * 注:本来以为结果是:RuntimeErrorException 结果是:ArithmeticException 查看了jre,我使用的是1.8 可能1.8异常捕获机制有所变化,不再支持父类型异常匹配子类型异常? * * */ public static void test2() { try { test1(); } catch(NullPointerException n) { System.out.println("NullPointerException"); } catch(RuntimeErrorException r) { System.out.println("RuntimeErrorException"); } catch(ArithmeticException a) { System.out.println("ArithmeticException"); } } /** * test3() 的运行结果与 test2()运行结果比较 可以看到java异常捕获的机制: * 本类 3 与 4 对比,想证明:父类型catch块可以成功捕获子类型异常,结果出乎意料。 * 运行结果:ArithmeticException * * * */ public static void test3() { try { test1(); } catch(NullPointerException n) { System.out.println("NullPointerException"); } catch(ArithmeticException a) { System.out.println("ArithmeticException"); } catch(RuntimeErrorException r) { System.out.println("RuntimeErrorException"); } } /** * try catch finally 连用,无异常抛出时,finally块和finally块后的代码会运行; * 运行结果:0 * finally 块中! * finally 块后! */ public static void test4() { try { test0();//不会抛出异常 } catch(NullPointerException n) { System.out.println("NullPointerException catch块中!"); } finally { System.out.println("finally 块中!"); } System.out.println("finally 块后!"); } /** * 如果try块中抛出了异常,catch块中捕获到了异常,finally块中的代码会运行,finally块后的代码会运行; * 运行结果:、 * ArithmeticException catch块中! *finally 块中! *finally 块后! */ public static void test5() { try { test1();//不会抛出异常 } catch(NullPointerException n) { System.out.println("NullPointerException catch块中!"); } catch(ArithmeticException a) { System.out.println("ArithmeticException catch块中!"); } finally { System.out.println("finally 块中!"); } System.out.println("finally 块后!"); } /** * 如果try块中抛出了异常,catch块中没有捕获到异常,finally块中的代码会运行,但是finally块后的代码不会运行; * finally 块中! Exception in thread "main" java.lang.ArithmeticException: / by zero at com.mjq.exceptiontest.ExceptionTest.test1(ExceptionTest.java:40) at com.mjq.exceptiontest.ExceptionTest.test6(ExceptionTest.java:160) at com.mjq.exceptiontest.ExceptionTest.main(ExceptionTest.java:151) */ public static void test6() { try { test1();//不会抛出异常 } catch(NullPointerException n) { System.out.println("NullPointerException catch块中!"); } finally { System.out.println("finally 块中!"); } System.out.println("finally 块后!"); } /*================================================================================================================== * 下面通过代码测试try catch finally 与return 一起使用后 * try 块,catch 块 finally块及finally块后的代码执行情况; * 7 不会抛出异常 * 8 会抛出异常 * * * * */ /** * 下面的方法不会抛出异常 * 打印运行返回结果: * Exception! * @return */ public static String test7() { String str = "Test Exception!"; return str.substring(4); } /** * 下面的代码会抛出异常 * 运行结果: * Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -5 * at java.lang.String.substring(Unknown Source) * at com.mjq.exceptiontest.ExceptionTest.test8(ExceptionTest.java:170) * at com.mjq.exceptiontest.ExceptionTest.main(ExceptionTest.java:175) * @return */ public static String test8() { String str = "Test Exception!"; return str.substring(20); } /*============================================================================ * 9 ----- 13 测试当try块中不抛出异常时,return的位置 * 结论:1.不论何种情况,finally块中的代码始终都会执行 * 2.try块未抛出异常的情况下,如果finally块中有return,则从finally块中返回;如果finally块中没有return,则检测try块中是否有renturn * 如果try块中有return,则从try块中返回;如果try块中没有return,则finallly块后的代码中肯定有return(不可能没有!),则从此处返回; * 3.catch 块中return ,在程序未抛出异常的情况下,并不会返回; * * * */ /** * return 存在位置 * 运行结果:finally块中! finally块后返回! * @return */ public static String test9() { try { test7();//不会抛出异常 } catch(StringIndexOutOfBoundsException s) { System.out.println("StringIndexOutOfBoundsException catch块中!"); } finally { System.out.println("finally块中!"); } return "finally块后返回!"; } /** * return 位置 * 运行结果:finally块中! Exception! * @return */ public static String test10() { try { return test7();//不会抛出异常 } catch(StringIndexOutOfBoundsException s) { System.out.println("StringIndexOutOfBoundsException catch块中!"); } finally { System.out.println("finally块中!"); } return "finally块后返回!"; } /** * return 位置 * 运行结果: * finally块中! finally块中返回! * @return */ public static String test11() { try { return test7();//不会抛出异常 } catch(StringIndexOutOfBoundsException s) { System.out.println("StringIndexOutOfBoundsException catch块中!"); } finally { System.out.println("finally块中!"); return "finally块中返回!"; } /*return "finally块后返回!";*/ } /** * return 位置 * 运行结果:finally块中! Exception! * @return */ public static String test12() { try { return test7();//不会抛出异常 } catch(StringIndexOutOfBoundsException s) { System.out.println("StringIndexOutOfBoundsException catch块中!"); return "StringIndexOutOfBoundsException catch块后返回!"; } finally { System.out.println("finally块中!"); } /*return "finally块后返回!";*/ } /** * return 位置 * finally块中! finally块中返回! * @return */ public static String test13() { try { return test7();//不会抛出异常 } catch(StringIndexOutOfBoundsException s) { System.out.println("StringIndexOutOfBoundsException catch块中!"); return "StringIndexOutOfBoundsException catch块后返回!"; } finally { System.out.println("finally块中!"); return "finally块中返回!"; } /*return "finally块后返回!";*/ } /**============================================================================== *14 --- 18 测试try块中抛出异常,catch块成功捕获异常时,return的不同位置 * 结论: * 1.try块中抛出异常,catch块中成功捕获异常,finally块中代码都会运行! * 2.代码执行顺序:首先执行try块中代码,如果抛出异常,则执行相应的catch块中的代码,然后执行finally块中的代码; * 检查catch块中是否有return,如果catch块中有return,则从该catch块中返回; * 如果catch块中木有return,则检查finally块中是否有return,如果有就从finally块中返回,如果没有则检查finally块后的代码中的return(此处一定会有!) * * **/ /** * return 存在位置 * 运行结果: * StringIndexOutOfBoundsException catch块中! finally块中! finally块后返回! * @return */ public static String test14() { try { test8();//抛出异常 } catch(StringIndexOutOfBoundsException s) { System.out.println("StringIndexOutOfBoundsException catch块中!"); } finally { System.out.println("finally块中!"); } return "finally块后返回!"; } /** * return 位置 * 运行结果: * StringIndexOutOfBoundsException catch块中! finally块中! finally块后返回! * @return */ public static String test15() { try { return test8();//抛出异常 } catch(StringIndexOutOfBoundsException s) { System.out.println("StringIndexOutOfBoundsException catch块中!"); } finally { System.out.println("finally块中!"); } return "finally块后返回!"; } /** * return 位置 * 运行结果: * StringIndexOutOfBoundsException catch块中! finally块中! finally块中返回! * @return */ public static String test16() { try { return test8();//抛出异常 } catch(StringIndexOutOfBoundsException s) { System.out.println("StringIndexOutOfBoundsException catch块中!"); } finally { System.out.println("finally块中!"); return "finally块中返回!"; } /*return "finally块后返回!";*/ } /** * return 位置 * 运行结果: * StringIndexOutOfBoundsException catch块中! finally块中! StringIndexOutOfBoundsException catch块后返回! * @return */ public static String test17() { try { return test8();//抛出异常 } catch(StringIndexOutOfBoundsException s) { System.out.println("StringIndexOutOfBoundsException catch块中!"); return "StringIndexOutOfBoundsException catch块中返回!"; } finally { System.out.println("finally块中!"); } /*return "finally块后返回!";*/ } /** * return 位置 * StringIndexOutOfBoundsException catch块中! finally块中! finally块中返回! * @return */ public static String test18() { try { return test8();//抛出异常 } catch(StringIndexOutOfBoundsException s) { System.out.println("StringIndexOutOfBoundsException catch块中!"); return "StringIndexOutOfBoundsException catch块后返回!"; } finally { System.out.println("finally块中!"); return "finally块中返回!"; } /*return "finally块后返回!";*/ } /*================================================================================ * 19 ----23 测试当try块中抛出异常,但是catch块中未能成功捕获时,return的不同位置返回 * 1.finally中的代码始终会运行; * 2.先执行代码,代码执行顺序: try块=====》catch块=====》finally块 * 3.然后检查return:检查顺序: finally块======》catch块 * * * * */ /** * return 存在位置 * 运行结果: * finally块中!Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -5 at java.lang.String.substring(Unknown Source) at com.mjq.exceptiontest.ExceptionTest.test8(ExceptionTest.java:205) at com.mjq.exceptiontest.ExceptionTest.test19(ExceptionTest.java:461) at com.mjq.exceptiontest.ExceptionTest.main(ExceptionTest.java:451) * @return */ public static String test19() { try { test8();//抛出异常 } catch(NullPointerException s) { System.out.println("NullPointerException catch块中!"); } finally { System.out.println("finally块中!"); } return "finally块后返回!"; } /** * return 位置 * 运行结果: * finally块中! Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -5 at java.lang.String.substring(Unknown Source) at com.mjq.exceptiontest.ExceptionTest.test8(ExceptionTest.java:205) at com.mjq.exceptiontest.ExceptionTest.test20(ExceptionTest.java:488) at com.mjq.exceptiontest.ExceptionTest.main(ExceptionTest.java:451) * @return */ public static String test20() { try { return test8();//抛出异常 } catch(NullPointerException s) { System.out.println("NullPointerException catch块中!"); } finally { System.out.println("finally块中!"); } return "finally块后返回!"; } public static void main(String [] args) { System.out.println(test23()); } /** * return 位置 * 运行结果: * finally块中! finally块中返回! * @return */ public static String test21() { try { return test8();//抛出异常 } catch(NullPointerException s) { System.out.println("NullPointerException catch块中!"); } finally { System.out.println("finally块中!"); return "finally块中返回!"; } /*return "finally块后返回!";*/ } /** * return 位置 * 运行结果: * StringIndexOutOfBoundsException catch块中! finally块中! StringIndexOutOfBoundsException catch块中返回! * @return */ public static String test22() { try { return test8();//抛出异常 } catch(StringIndexOutOfBoundsException s) { System.out.println("StringIndexOutOfBoundsException catch块中!"); return "StringIndexOutOfBoundsException catch块中返回!"; } finally { System.out.println("finally块中!"); } /*return "finally块后返回!";*/ } /** * return 位置 * 运行结果: * StringIndexOutOfBoundsException catch块中! finally块中! finally块中返回! * @return */ public static String test23() { try { return test8();//抛出异常 } catch(StringIndexOutOfBoundsException s) { System.out.println("StringIndexOutOfBoundsException catch块中!"); return "StringIndexOutOfBoundsException catch块后返回!"; } finally { System.out.println("finally块中!"); return "finally块中返回!"; } /*return "finally块后返回!";*/ } }
相关文章推荐
- javaSE_8系列博客——重要的基础Java类——Excavations(异常处理机制)--2--什么是异常?
- javaSE_8系列博客——重要的基础Java类——Excavations(异常处理机制)--3--捕捉异常或指定要求
- Java学习系列(九)Java面向对象之异常机制详解
- javaSE_8系列博客——重要的基础Java类——Exceptions(异常处理机制)--4--总结
- JavaSE系列第二篇——java环境配置之为什么?
- Java学习系列(九)Java面向对象之异常机制详解
- javaSE_8系列博客——重要的基础Java类——Excavations(异常处理机制)--1--宏观把控
- 【javaSE】java异常处理机制
- java异常机制分析
- Java的异常机制详解
- C++的和Java的异常机制
- Java中异常机制的深入研究
- Java中的异常处理机制
- Java中异常机制的深入研究
- 关于java异常处理机制的深入理解(转)
- JAVA异常处理机制
- Java异常机制
- JAVA进阶:Java中异常机制的深入研究
- java.util.concurrent系列文章--(2)JDK1.5 锁机制
- 深入Java的异常机制