NullPointerException和IllegalAccessException的不同行为
我试图理解JAVA中的throws子句,我写了以下内容NullPointerException和IllegalAccessException的不同行为,exception,nullpointerexception,throws,illegalaccessexception,Exception,Nullpointerexception,Throws,Illegalaccessexception,我试图理解JAVA中的throws子句,我写了以下内容 一段代码: class check { static void demo() { System.out.println("Hello\n"); throw new IllegalAccessException("demo"); } public static void main(String args[]) { demo(); } } 我知道
一段代码:
class check
{
static void demo()
{
System.out.println("Hello\n");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
demo();
}
}
我知道它不会编译,因为必须处理异常在main方法和demo()中,应通过指定抛出来定义
非法准入例外条款 但是当我将异常更改为NullPointerException时,相同的
程序编译并执行:
class check
{
static void demo()
{
System.out.println("Hello\n");
throw new NullPointerException("Demo");
}
public static void main(String args[])
{
demo();
}
}
是的,情况就是这样。谢谢