Java 在没有“捕获”的情况下进行“最终尝试”是否有意义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2614473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does it make sense to do "try-finally" without "catch"?
提问by glanden
I saw some code like this:
我看到了一些这样的代码:
try
{
db.store(mydata);
}
finally
{
db.cleanup();
}
I thought try
is supposed to have a catch
?
我以为try
应该有一个catch
?
Why does this code do it this way?
为什么这段代码会这样做?
采纳答案by Taylor Leese
This is useful if you want the currently executing method to still throw the exception while allowing resources to be cleaned up appropriately. Below is a concrete example of handling the exception from a calling method.
如果您希望当前执行的方法仍然抛出异常,同时允许适当地清理资源,这将非常有用。下面是处理来自调用方法的异常的具体示例。
public void yourOtherMethod() {
try {
yourMethod();
} catch (YourException ex) {
// handle exception
}
}
public void yourMethod() throws YourException {
try {
db.store(mydata);
} finally {
db.cleanup();
}
}
回答by Matti Virkkunen
It's there because the programmer wanted to make sure that db.cleanup()
is called even if the code inside the try block throws an exception. Any exceptions will not be handled by that block, but they'll only be propagated upwards after the finally block is executed.
它存在是因为程序员想要确保db.cleanup()
即使 try 块中的代码抛出异常也能调用它。该块不会处理任何异常,但它们只会在执行 finally 块后向上传播。
回答by Konrad Rudolph
Why does this code do it this way?
为什么这段代码会这样做?
Because apparently the code doesn't know how to handle exceptions at this level. That's fine– as long as one of the callers does, i.e. as long as the exception gets ultimately handled somewhere.
因为显然代码不知道如何处理这个级别的异常。这很好——只要调用者之一这样做,即只要异常最终在某个地方得到处理。
Often, low-level code cannot react appropriately to exceptions because the user needs to be notified, or the exception must be logged, or another strategy has to be tried. Low-level code performs onefunction only and doesn't know about higher-level decision making.
通常,低级代码无法对异常做出适当的反应,因为需要通知用户,或者必须记录异常,或者必须尝试其他策略。低级代码只执行一项功能,不知道更高级别的决策。
But the code still needs to clean up its resources (because if it doesn't, they would leak), so it does just that in the finally
clause, making sure that it alwayshappens, whether an exception was thrown or not.
但是代码仍然需要清理它的资源(因为如果不清理,它们会泄漏),所以它只是在finally
子句中这样做,确保它总是发生,无论是否抛出异常。
回答by FRotthowe
The finally block ensures that even when a RuntimeException is thrown (maybe due to some bug in the called code), the db.cleanup()
call will be made.
finally 块确保即使抛出 RuntimeException(可能是由于被调用代码中的某些错误),db.cleanup()
也会进行调用。
This is also often used to prevent too much nesting:
这也常用于防止过多的嵌套:
try
{
if (foo) return false;
//bla ...
return true;
}
finally
{
//clean up
}
Especially when there are many points at which the method returns, this improves readability as anyone can see the clean up code is called in every case.
特别是当方法返回的点很多时,这提高了可读性,因为任何人都可以看到在每种情况下都调用了清理代码。
回答by chustar
The code is doing it to ensure that the database is closed.
Usually, the way you would do it is to put all your database accessing code in the try block, and then put a call to close the database in the finally block.
The way try...finally works, means that the code in the try block is run, and the code in the finally block is run when that finishes...no matter what.
Short of the computer being yanked from the wall, the finally will execute.
This means that even if an exception is called, and the method takes three years to execute, it will still go in the finally block and the database will be closed.
代码这样做是为了确保数据库已关闭。
通常,您的做法是将所有数据库访问代码放在 try 块中,然后在 finally 块中调用关闭数据库。
try...finally 工作的方式意味着 try 块中的代码被运行,并且 finally 块中的代码在完成时运行......无论如何。
没有从墙上猛拉计算机,最终将执行。
这意味着即使调用了异常,并且该方法需要三年时间才能执行,它仍然会进入 finally 块并关闭数据库。
回答by THINESH VASEE
If any of the code in the try block can throw a checked exception, it has to appear in the throws clause of the method signature. If an unchecked exception is thrown, it's bubbled out of the method.
如果 try 块中的任何代码可以抛出已检查的异常,则它必须出现在方法签名的 throws 子句中。如果抛出未经检查的异常,它会从方法中冒泡出来。
The finally block is always executed, whether an exception is thrown or not.
无论是否抛出异常,finally 块都会被执行。