默认情况下,将本地变量声明为 final
是一种常见的“良好实践”。不知道Eclipse是否有此功能,但在IDEA中,“创建本地变量”对话框中甚至有一个复选框。但是有一个问题让我无法始终使用它。以这段代码为例:
...
final Foo foo = null;
try{
foo = getFromSomewhere();
} catch (IDontCareException e) {
log.info(e, "looks like foo is not there);
}
if (foo != null) {
doSomethingWithFoo(foo);
}
doSomethingElse();
...
问题在于
IDontCareException
没有继承 RuntimeException
... 是否有办法仍然使用 final
变量?
IDontCareException e
标记为final
,因为IDontCareException
没有扩展RuntimeException
? - sigpwned