Types of try-with-resource statements in Java 7 - Java @ Desk

Thursday, July 31, 2014

Types of try-with-resource statements in Java 7

Types of try-with-resource statements in Java 7

In our last post, we learned the basics of "Automatic Resource Management - try-with-resources Statement"

basic try-with-resources statement : A try-with-resources statement with no catch clauses or finally clause is called a basic try-with-resources statement.

try (VariableModifiers R Identifier = Expression ...)
    Block


extended try-with-resources statement : 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 (VariableModifiers R Identifier = Expression)
    Block
[Catches]
[Finally]


is given by the following translation to a basic try-with-resources statement nested inside a try-catch or try-finally or try-catch-finally statement:

try {
    try (VariableModifiers R Identifier = Expression)
        Block
}
[Catches]
[Finally]



The meaning of an extended try-with-resources statement with multiple resources:

try (VariableModifiers R Identifier1 = Expression;
VariableModifiers R Identifier2 = Expression;)
    Block
Catches
Finally


is given by the following translation to a basic try-with-resources statement nested inside a try-catch or try-finally or try-catch-finally statement:

try {
    try (VariableModifiers R Identifier1 = Expression)
        Block
}
[Catches]
[Finally]

try {
    try (VariableModifiers R Identifier2 = Expression)
        Block
}
[Catches]
[Finally]


This post is written by Dipika Mulchandani. She is a freelance writer, loves to explore latest features in Java technology.





No comments:

Post a Comment