Class TryWithResourcesStatementExamples

java.lang.Object
net.jrodolfo.java_evolution.java07.TryWithResourcesStatementExamples

public class TryWithResourcesStatementExamples extends Object
Demonstrates try-with-resources, introduced in Java 7.
  • Constructor Details

    • TryWithResourcesStatementExamples

      public TryWithResourcesStatementExamples()
  • Method Details

    • readFirstLine

      public String readFirstLine(String text) throws IOException
      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

      public boolean readFirstLineClosesResource(String text) throws IOException
      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

      public void failWithSuppressedCloseFailure() throws Exception
      Shows that cleanup failures are preserved as suppressed exceptions when the main operation already failed.
      Throws:
      Exception - always throws the primary operation failure