【发布时间】:2016-06-29 01:46:22
【问题描述】:
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,他问我以下有关此源的问题。理论上这必须throw IOException("Exception rised."),整个方法retIntExc() 必须throws Exception。但是什么也没发生,该方法返回 2。
我没有测试过他的例子,但我认为这不是预期的行为。
编辑:感谢所有答案。你们中的一些人忽略了方法称为retIntExc 的事实,这意味着这只是一些测试/实验示例,显示了投掷/捕捉机制的问题。我不需要“修复”,我需要解释为什么会发生这种情况。
【问题讨论】:
-
为什么不声明
throws IOException? -
@BoltClock,这有什么关系?