static int retIntExc() throws Exception{
int result = 1;
try {
result = 2;
throw new IOException("Exception rised.");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
result = 3;
} finally {
return result;
}
}
我的一位朋友是.NET开发人员,目前正在迁移到Java,他向我提出了关于此源代码的问题。理论上这必须会抛出IOException("Exception rised.")并且整个方法retIntExc()必须抛出Exception。但是没有任何反应,该方法返回2。
我还没有测试过他的示例,但我认为这不是期望的行为。
编辑:感谢所有答案。你们中的一些人忽略了方法名为retIntExc的事实,这意味着这只是一些测试/实验性的示例,展示了抛出/捕获机制中的问题。我不需要“修复”,我需要解释为什么会发生这种情况。