Class TryWithResourcesStatementExamples
java.lang.Object
net.jrodolfo.java_evolution.java07.TryWithResourcesStatementExamples
Demonstrates try-with-resources, introduced in Java 7.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidShows that cleanup failures are preserved as suppressed exceptions when the main operation already failed.readFirstLine(String text) Reads the first line from text using a resource declared inside the try-with-resources header.booleanUses a tracking resource so a caller can observe automatic closing.
-
Constructor Details
-
TryWithResourcesStatementExamples
public TryWithResourcesStatementExamples()
-
-
Method Details
-
readFirstLine
Reads the first line from text using a resource declared inside the try-with-resources header.- Parameters:
text- text to read- Returns:
- the first line
- Throws:
IOException- when reading fails
-
readFirstLineClosesResource
Uses a tracking resource so a caller can observe automatic closing.- Parameters:
text- text to read- Returns:
- whether the resource was closed after the try block
- Throws:
IOException- when reading fails
-
failWithSuppressedCloseFailure
-