先看下面的代码,看看程序执行会是什么样的结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.lang.reflect.Method; /** * @author Unmi */ public class ExceptionTest { public static void main(String[] args) { try{ foo1(); }catch (MyException me) { System.out.println(“Exception Type: MyException”); }catch (Exception e) { System.out.println(“Exception Type: Exception”); } } public static void foo1() throws Exception{ Method method = ExceptionTest.class.getDeclaredMethod(“foo2″,new Class[]{}); //注意调用foo2时,foo2方法会抛出MyException异常 method.invoke(null,new Object[]{}); }<!--more--> public static void foo2() throws Exception{ throw new MyException(); //foo2方法直接抛出异常 } } //一个自定义的异常 class MyException extends Exception{ } |