我试图理解 JAVA 中的 throws 子句,我编写了以下
代码:
class check
{
static void demo()
{
System.out.println("Hello\n");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
demo();
}
}
我知道它不会编译,因为必须
在主方法中处理异常,并且应该通过
为 IllegalAccessException 指定 throws 子句来定义 demo()。
但是当我将异常更改为 NullPointerException 时,相同的
程序会编译并执行:
class check
{
static void demo()
{
System.out.println("Hello\n");
throw new NullPointerException("Demo");
}
public static void main(String args[])
{
demo();
}
}