这样就可以在方法调用的时候捕获它。
代码如下:
public class ThrowsDemo { static void throwOne() throws IllegalAccessException { System.out.println("Inside throwOne."); throw new IllegalAccessException("demo"); } public static void main(String args[]) { try { throwOne(); } catch (IllegalAccessException e) { System.out.println("Caught " + e); } } }
运行结果:
Inside throwOne.
Caught java.lang.IllegalAccessException: demo