Consider the following program:
import java.util.Scanner; public class Main { public static void main(String []args) { try (Scanner consoleScanner = new Scanner(System.in)) { consoleScanner.close(); // CLOSE consoleScanner.close();/*from w w w. j a v a 2 s .co m*/ } } }
Which one of the following statements is correct?
A. this program terminates normally without throwing any exceptions B. this program throws an IllegalStateException C. this program throws an IOException d. this program throws an AlreadyClosedException e. this program results in a compiler error in the line marked with the comment CLOSE
A.
the try-with-resources statement internally expands to call the close()
method in the finally block.
if the resource is explicitly closed in the try block, then calling close()
again does not have any effect.