class ThrowsDemo{
static void throwMethod() {
System.out.println("inside throwMethod");
//throw new IllegalAccessException("demo");
}
public static void main(String arg[]){
try{
throwMethod();
}catch(IllegalAccessException e){ //改成(Exception e)就对了。不晓得为什么?
System.out.println("Caught" +e);
}
}
}
static void throwMethod() {
System.out.println("inside throwMethod");
//throw new IllegalAccessException("demo");
}
public static void main(String arg[]){
try{
throwMethod();
}catch(IllegalAccessException e){ //改成(Exception e)就对了。不晓得为什么?
System.out.println("Caught" +e);
}
}
}
static void throwMethod() throws IllegalAccessException {
System.out.println("inside throwMethod");
throw new IllegalAccessException("demo");}public static void main(String arg[]){
try{
throwMethod();
}catch(IllegalAccessException e){ //改成(Exception e)就对了。不晓得为什么?
System.out.println("Caught" +e);
}
}
}
public class IllegalAccessException
extends Exception
当应用程序试图反射性地创建一个实例(而不是数组)、设置或获取一个字段,或者调用一个方法,但当前正在执行的方法无法访问指定类、字段、方法或构造方法的定义时,抛出 IllegalAccessException。 我把你的程序改成如下的代码,它可以正常编译运行。
class ThrowsDemo{
static void throwMethod() {
System.out.println("inside throwMethod");
//throw new IllegalAccessException("demo");}public static void main(String arg[]){
//try{
throwMethod();
//}catch(IllegalAccessException e){ //改成(Exception e)就对了。不晓得为什么?
//System.out.println("Caught" +e);
//}
}
}
你想一下,这个原因不就很清楚了吗?