Nipun Alahakoon
Nipun Alahakoon

Reputation: 2862

Running a program consist of try block but without catch or final clauses

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?

Upvotes: 2

Views: 79

Answers (2)

Mananpreet Singh
Mananpreet Singh

Reputation: 295

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

Upvotes: 1

Codebender
Codebender

Reputation: 14471

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.

Upvotes: 5

Related Questions