mirror of
https://github.com/Snailclimb/JavaGuide
synced 2025-06-16 18:10:13 +08:00
commit
d659762e0e
@ -1301,7 +1301,7 @@ public class Test {
|
||||
1. **适用范围(资源的定义):** 任何实现 `java.lang.AutoCloseable`或者 `java.io.Closeable` 的对象
|
||||
2. **关闭资源和 finally 块的执行顺序:** 在 `try-with-resources` 语句中,任何 catch 或 finally 块在声明的资源关闭后运行
|
||||
|
||||
《Effecitve Java》中明确指出:
|
||||
《Effective Java》中明确指出:
|
||||
|
||||
> 面对必须要关闭的资源,我们总是应该优先使用 `try-with-resources` 而不是`try-finally`。随之产生的代码更简短,更清晰,产生的异常对我们也更有用。`try-with-resources`语句让我们更容易编写必须要关闭的资源的代码,若采用`try-finally`则几乎做不到这点。
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user