import java.util.InputMismatchException;
public class quiz {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
System.out.printf("%d%n",f(6,0));
}
catch (InputMismatchException e) {
System.out.println("catch 1");
}
catch (Exception e) {
System.out.println("catch 2");
}
finally {
System.out.println("finally");
}
}
private static int f(int a, int b) {
return a/b;
}
}
注意:不是“它将以算术异常终止”。
假定已包含所有必需的库,在这种情况下,java.util包中包含InputMismatchException。
在try块中,您正在调用尝试将数字除以0的静态方法,它将引发ArithmetciException,并且由于它不是在static方法f中处理的,因此它进入main方法,即main方法的第一个catch块。是用于捕获InputMismacthcException的,但是生成的异常是ArithmeticException,因此它进入下一个捕获,即cathc2。因此,捕获2将被打印在屏幕上。无论发生什么,finally块都会被执行。因此,我们也最终将其打印在屏幕上。
因此,我们得到的输出是:
catch 2
finally