简体   繁体   中英

Java: IllegalAccessException

There is an error while compiling this. Whereas there is no error when I try to throw a NullPointerException. Could anyone please help me on this one? Thanks.

class ThrowsDemo {
    static void throwOne(){
        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);
        }
    }
}

因为IllegalAccessException是一个已检查的异常,所以您必须向throwOne()添加throws IllegalAccessException ,以通知任何调用方法它们需要处理它。

static void throwOne() throws IllegalAccessException {

Because IllegalAccessException is chceked exception you must declare that your method can throws this exception. Just add to throwOne method definition 'throws IllegalAccesException'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM