【发布时间】:2016-04-17 13:24:48
【问题描述】:
如果我有方法:
public static boolean getA() throws Exception{
try{
throw new Exception();
}finally
{
}
}
不需要返回语句。此外,如果我们尝试在末尾添加 return 语句,则会产生“unreachable statement”错误。
为什么会这样?确定程序不会出块,会抛出异常吗?
此外,如果我们添加一个 catch 块而不是 finally 块,那么它需要 return 语句出现在那里。
【问题讨论】:
-
编译器知道无条件异常使进一步的执行变得不可能,更多的代码无法执行。
return是必需的,因为返回类型是boolean -
不可能将代码放在
throws之后,因此编译器不希望您这样做。