【发布时间】:2015-01-17 12:34:03
【问题描述】:
我知道在 finally 块中返回会导致不希望的结果,例如丢失未捕获的异常。但我在这里想了解的是 Eclipse 对警告 "finally block does not complete normally" 的建议修复。
考虑这段代码sn-p:
catch (SQLException se)
{
System.out.println("Exception occured in the database");
System.out.println("Exception message: "+ se.getMessage());
System.out.println("Database error code: "+ se.getErrorCode());
se.printStackTrace();
}
finally
{
// Clean up
if(callStmt != null)
{
try
{
callStmt.close();
}
catch (SQLException se2)
{
se2.printStackTrace();
}
}
if(conn != null)
{
try
{
conn.close();
}
catch (SQLException se2)
{
se2.printStackTrace();
}
}
return newEmpSalary;
}
}
对于上面的代码 Eclipse Luna 给出警告 "finally block does not complete normally" ,建议的修复是:
应用上述修复仍然不会使警告消失。我知道我只需要删除 return 语句即可摆脱它。
但我很想知道为什么 Eclipse 在这种情况下建议修复 "Change modifiers to final where possible"。
【问题讨论】:
-
不知道为什么这么说......显而易见的解决方案是将 return 语句从 finally 块中移出。
标签: java eclipse exception return try-catch-finally