import java.io.IOException;
public class ExceptionTest{
public static void methodA(){
throw new IOException();
}
public static void main (String[] args){
try {
methodA();
} catch (IOException e) {
System.out.println("Caught IOException");
} catch (Exception e) {
System.out.println("Caught Exception");
}
}
}what is the result?
A.The code will not compile.
B.The output is caught exception.
C.The output is caught IOException.
D.The program executes normally without printing a message. 结果是什么,原因??
因为IOException是checked-exception
所以methodA()必须声明throws块
对于方法体中的 “非运行时异常” ,方法本身要么捕获处理,要么抛出
而 运行时异常 ,则无此限制,因为java的运行时系统会自动捕获并处理
因此以上程序不会通过编译
所以methodA()必须声明throws块
是这样的