最终不经尝试就封锁是否有效? [英] Is it valid to have finally block without try and catch?
问题描述
我正在尝试使用finally块而不使用try/catch块,但是在Eclipse中出现错误.
I am trying to use the finally block without using the try/catch blocks but getting the error in Eclipse.
我可以在不使用try/catch块的情况下使用finally块吗?
Can I use the finally block without using the try/catch blocks?
推荐答案
finally
应该至少有一个try
块,catch
是可选的. finally块的目的是确保无论是否引发异常,都对东西进行清理.根据 JLS
finally
should have atleast a try
block, catch
is optional. The point of finally blocks is to make sure stuff gets cleaned up whether an exception is thrown or not. As per the JLS
finally子句可确保在try块和可能执行的任何catch块之后之后执行finally块,无论控制如何离开try块或catch块.
A finally clause ensures that the finally block is executed after the try block and any catch block that might be executed, no matter how control leaves the try block or catch block.
因此,finally
应该始终在try
块之前.
Hence a finally
should always be preceded by a try
block.
这篇关于最终不经尝试就封锁是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!