You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* “So what should you do instead of writing a finalizer or cleaner for a class whose objects encapsulate resources that require termination, such as files or threads? Just **have your class implement `AutoCloseable`**, and require its clients to invoke the `close` method on each instance when it is no longer needed, typically using `try`-with-resources to ensure termination even in the face of exceptions (Item 9).”
208
208
* “One detail worth mentioning is that the instance must keep track of whether it has been closed: the `close` method must record in a field that the object is no longer valid, and other methods must check this field and throw an `IllegalStateException` if they are called after the object has been closed.”
209
209
***“In summary, don’t use cleaners, or in releases prior to Java 9, finalizers, except as a safety net or to terminate noncritical native resources.”**
210
+
211
+
## Item 9: Prefer `try`-with-resources to `try`-`finally`
212
+
213
+
* “Historically, a try-finally statement was the best way to guarantee that a resource would be closed properly, even in the face of an exception or return.”
214
+
215
+
```java
216
+
// try-finally - No longer the best way to close resources!
* “To be usable with the `try`-with-resources statement, a resource must implement the AutoCloseable interface, which consists of a single void-returning `close` method.”
228
+
229
+
```java
230
+
// try-with-resources on multiple resources - short and sweet
***“Always use `try`-with-resources in preference to `try`-`finally` when working with resources that must be closed. The resulting code is shorter and clearer, and the exceptions that it generates are more useful.”**
0 commit comments