3
考虑这个问题,有人问我,在接受采访时当catch块和finally块在Java中抛出异常时会发生什么?
public class Test_finally {
private static int run(int input) {
int result = 0;
try {
result = 3/input;
} catch (Exception e) {
System.out.println("UnsupportedOperationException");
throw new UnsupportedOperationException("first");
} finally {
System.out.println("finally input=" + input);
if (0 == input) {
System.out.println("ArithmeticException");
throw new ArithmeticException("second");
}
}
System.out.println("end of method");
return result * 2;
}
public static void main(String[] args) {
int output = Test_finally.run(0);
System.out.println(" output=" + output);
}
}
这个节目的输出抛出ArithmeticException
不UnsupportedOperationException
记者简单地问我将如何让客户知道提出的原始异常是UnsupportedOperationException
型不ArithmeticException
的。 我不知道
人们可能不会刻意从'finally'块抛出,但多种因素可导致选中(和意外)例外'finally'块内发生。 – supercat