package ExceptSourceCode; 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"); } } }
输出:
in Level 1
in Level 2
in Level 3
Level 3:class java.lang.ArithmeticException
In Level 3 finally
In Level 2 finally
In Level 1 finally
执行了第三层的catch和后续的finally中代码,
当把第三层catch注释掉后
package ExceptSourceCode; 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 } 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"); } } }
输出:
in Level 1
in Level 2
in Level 3
In Level 3 finally
Level 2:class java.lang.ArithmeticException
In Level 2 finally
In Level 1 finally
所以,try catch嵌套,内层不能捕获时,会考虑外层内否捕获,内层能捕获,则外层catch不执行。
-------------------------------
package ExceptSourceCode; 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"); } } }
输出:
in main
Exception is thrown in main
该代码中finally没有被执行,因为在catch中有System.exit(0);当执行到这条语句时,JVM关闭,程序停止。
一个方法可以申明抛出多个异常
package ExceptSourceCode; import java.io.*; public class ThrowMultiExceptionsDemo { public static void main(String[] args) { try { throwsTest(); } catch(IOException e) { System.out.println("捕捉异常"); } } private static void throwsTest() throws ArithmeticException,IOException { System.out.println("这只是一个测试"); // 程序处理过程假设发生异常 throw new IOException(); //throw new ArithmeticException(); } }
一个子类的throws子句抛出的异常,不能是其基类同名方法抛出的异常对象的父类。
package ExceptSourceCode; import java.io.*; public class OverrideThrows { public void test()throws IOException { FileInputStream fis = new FileInputStream("a.txt"); } } class Sub extends OverrideThrows { //如果test方法声明抛出了比父类方法更大的异常,比如Exception //则代码将无法编译…… public void test() throws FileNotFoundException { //... } }
自定义异常通常选择直接派生自Exception
Class MyException extends Exception { … }
在合适的地方使用throw语句抛出自定义异常对象
Class MyClass { void someMethod(){ if (条件) throw new MyException(); } }