在研究了越来越多关于throws
声明的内容后,Exception Handling
我感到困惑。我发现-
如果方法能够导致它无法处理的异常,则它必须指定此行为,以便方法的调用者可以保护自己免受该异常的影响。
class Boxing1{
public static void main(String args[]) throws IOException{
new B().meth2();
System.out.println("33333333");
}
}
class A{
void meth1() throws IOException{
throw new IOException();
//throw new NullPointerException();
//System.out.println("111111111111");
}
}
class B{
void meth2() throws IOException{
new A().meth1();
System.out.println("2222222");
}
}
而不是使用 throws 仍然存在异常-我的控制台显示以下错误-
Exception in thread "main" java.io.IOException
at A.meth1(Boxing1.java:17)
at B.meth2(Boxing1.java:24)
at Boxing1.main(Boxing1.java:10)
直到我没有在 try-catch 块中调用 meth1,尽管使用了 throws,但仍然存在异常。throws在这里的作用是什么?
try{new A().meth1();}catch(Exception e){System.out.println(e);}
我需要你的确认。我很困惑。我的单行查询是-
throws
除了传播 a之外,还有其他角色CheckedException
吗?