这是正确的,没有catch子句的要求。 Oracle java 7 doc表示资源将被关闭,而不管是否实际抛出异常。
只有当您想对异常作出反应时,才应使用catch子句。 catch子句将在资源关闭后执行。
The following example reads the first line from a file. It uses an
instance of BufferedReader to read data from the file. BufferedReader
is a resource that must be closed after the program is finished with
it:
static String readFirstLineFromFile(String path) throws IOException {
try (BufferedReader br =
new BufferedReader(new FileReader(path))) {
return br.readLine();
}
} // In this example, the resource declared in the try-with-resources statement is a BufferedReader.
… Because the BufferedReader instance is declared in a
try-with-resource statement, it will be closed regardless of whether
the try statement completes normally or abruptly (as a result of the
method BufferedReader.readLine throwing an IOException).
编辑
关于新编辑的问题:
Java 6中的代码执行catch和之后的finally块。这导致资源仍然可能在catch块中打开。
在Java 7语法中,资源在catch块之前关闭,因此资源在catch块执行期间已经关闭。这在上面的链接中记录:
In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.