Usually this would give syntax error. But in the oracle tutorial in try-with-resources
Statement section , there are several code samples with try
block but without catch
or finally
statements. How come this codes does not give syntax errors?

- 17,525
- 6
- 57
- 76

- 2,772
- 5
- 27
- 45
-
3Because it was added to the language spec like that in Java 7. – Erwin Bolwidt Jan 06 '16 at 07:21
-
Why do you believe they should cause a syntax error? A new syntax has new rules, is that so surprising? – Peter Lawrey Jan 06 '16 at 07:23
-
2The likelihood is, the method has been declared to throw the resulting exception, try-with-resources is (effectively) the same as try-finally – MadProgrammer Jan 06 '16 at 07:23
-
1Possible Duplicate of http://stackoverflow.com/questions/4559661/java-try-catch-finally-blocks-without-catch – Amit Jain Jan 06 '16 at 07:26
2 Answers
In try with resources block, an implicit finally
is added with code calling the close()
method on all Closable
instances...
So, a finally
is still present in the block. So it's syntactically correct.

- 14,221
- 7
- 48
- 85
-
1@Optimuskck new as in Java 7 is so old, it isn't publicly supported ;) – Peter Lawrey Jan 06 '16 at 07:24
The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.
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. The declaration statement appears within parentheses immediately after the try keyword. The class BufferedReader, in Java SE 7 and later, implements the interface java.lang.AutoCloseable. 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).
Read more here : https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
Quote from http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.20.3.2:
14.20.3.2 Extended try-with-resources
A try-with-resources statement with at least one catch clause and/or a finally clause is called an extended try-with-resources statement. The meaning of an extended try-with-resources statement:
try ResourceSpecification Block Catches//opt Finally//opt
is given by the following translation to a basic try-with-resources statement (§14.20.3.1) nested inside a try-catch or try-finally or try-catch-finally statement:
try { try ResourceSpecification Block } Catches//opt Finally//opt
The effect of the translation is to put the ResourceSpecification "inside" the try statement. This allows a catch clause of an extended try-with-resources statement to catch an exception due to the automatic initialization or closing of any resource. So, basically, wrapper is already implemented

- 295
- 1
- 9