最终不经尝试就封锁是否有效? [英] Is it valid to have finally block without try and catch?

查看:49
本文介绍了最终不经尝试就封锁是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆