Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java try-with-resources syntax irregularity

So I was looking through some of the new features of java 7, including the try-with-resources bit.

I understand how it works and everything, I just noticed that the syntax used to specify the resources is a little odd.

try 
    (InputStream fis = new FileInputStream(source);
        OutputStream fos = new FileOutputStream(target))
    {
            // stuff
        }
    }
    catch (Exception e) {
        // stuff
    }

Specifically the definition of resources:

try (InputStream fis = new FileInputStream(source);
        OutputStream fos = new FileOutputStream(target))

Is there any other place in java where separating statements within a parenthesis block is valid?

The only other time I can think of is a for loop

for ( ; ; )

but that's not quite the same since there has to be exactly 2 ;s, and statements are separated with a , as in

for (int i = 1,  j = 100;  i <= 100, j > 0;  i = i-1, j = j-1)

So my question is, where did this syntax come from? Is there a reason the statements are ; delimited instead of , delimited? Is there even another comparable language that has a similar use of ; separated statements inside of a () block? I can't think of an example in java, C, or python.

like image 978
Falmarri Avatar asked Jul 28 '11 23:07

Falmarri


People also ask

Which of the following is the correct syntax of try with resources?

The try-with-resources statement automatically closes all the resources at the end of the statement. A resource is an object to be closed at the end of the program. As seen from the above syntax, we declare the try-with-resources statement by, declaring and instantiating the resource within the try clause.

What happens when an exception is thrown from the try with resources?

If an exception is thrown from within a Java try-with-resources block, any resource opened inside the parentheses of the try block will still get closed automatically. The throwing of the exception will force the execution to leave the try block, and this will force the automatic closing of the resource.

How do you handle exceptions in trying with resources?

For try-with-resources, if an exception is thrown in a try block and in a try-with-resources statement, then the method returns the exception thrown in the try block. The exceptions thrown by try-with-resources are suppressed, i.e. we can say that try-with-resources block throws suppressed exceptions.

Can multiple resources be used in try with resources?

We can declare multiple resources in a try block. Try initialization block can have any number of resources resulting in either null or non-null resources.


1 Answers

In general, statements are terminated with semicolons in Java. Note that try-with-resources differs from an assignment like int i = 1, j = 100; because it doesn't require that each thing being initialized be of the same type. It's really just a series of assignment statements wrapped in parentheses.

That said, I don't think there really needs to be any precedent for using a certain syntax if it's easily understood.

like image 167
ColinD Avatar answered Oct 18 '22 22:10

ColinD