Try-with-resources scope of resource

前端 未结 4 1525
旧巷少年郎
旧巷少年郎 2021-01-07 23:43

In the try-with-resources construct of Java 7, I can declare a resource in the try statement, and it will be closed autom

相关标签:
4条回答
  • 2021-01-08 00:24

    The correct scope limitation is within the declaration part (...) and the actual try block.

    The JLS states

    The scope of a variable declared in the ResourceSpecification of a try-with-resources statement (§14.20.3) is from the declaration rightward over the remainder of the ResourceSpecification and the entire try block associated with the try-with-resources statement.

    So from the point it is declared in the ResourceSpecification (...) of the try onwards until the final closing } bracket of the try Block.

    TryWithResourcesStatement:
        try ResourceSpecification Block Catchesopt Finallyopt
    
    ResourceSpecification:
        ( Resources ;opt )
    
    Resources:
        Resource
        Resource ; Resources
    
    Resource:
        VariableModifiersopt Type VariableDeclaratorId = Expression
    
    0 讨论(0)
  • 2021-01-08 00:25

    This syntax is called Extended try-with-resources

    As per JLS:

    try ResourceSpecification
        Block
    Catchesopt
    Finallyopt
    

    Will be translated to:

    try {
        try ResourceSpecification
            Block
    }
    Catchesopt
    Finallyopt
    

    So, in your example, your resource will be limited to inner try block, so not available for outer try/catch/finally.

    EDIT:

    my question does not have nested try blocks

    By explicitly adding catch/finally block in your code, you are introducing nested try blocks.

    0 讨论(0)
  • 2021-01-08 00:28

    Update from 2017 after Java 9 release

    Now with Java 9 we have more syntactic sugar and we can have a resource declared outside the try-catch block but still handled properly. That's why with Java 9 the Try-With-Resources has been improved introducing a new syntax:

    InputStream stream = new MyInputStream(...)
    try (stream) {
       // do something with stream being sure that is going to be closed at the end
    } catch(IOException e) {
       // you can surely use your resource here
    }
    

    Note that this syntax will result in a compile time error for Java version 8 or minor

    This is more "natural" way of writing even though in most use cases we don't need the resource outside the scope of the try block. The only restriction is that the reader variable should be effectively final or just final.

    Anyway with this syntax you can surely have your resource used also in the catch and finally block

    0 讨论(0)
  • 2021-01-08 00:30

    In addition to @Nambari's answer:

    A try-with-resources statement can have catch and finally blocks just like an ordinary try statement. In a try-with-resources statement, any catch or finally block is run after the resources declared have been closed.

    That pretty much explains the behaviour, your resource goes out of scope in your explicit catch/finally block.

    Reference

    0 讨论(0)
提交回复
热议问题