JDK-6980056 : var in resource specification can be redeclared as exception parameter in catch clause
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 7
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: generic
  • CPU: generic
  • Submitted: 2010-08-26
  • Updated: 2010-09-02
  • Resolved: 2010-09-02
Related Reports
Relates :  
Description
ccc write-up specifies:
"...the name of the variable may not be...redeclared as an exception parameter of a catch clause in a try statement of the directly enclosing method or initializer block"
but this code compiles without error:
public class negtest06 {
  String test1( ) {
    String ret = null;
    try {
      try ( java.io.FileInputStream str = new java.io.FileInputStream("foo.txt") ) {
        ret = str.getClass().getSimpleName();
      } catch( Exception str) {}
    } catch ( Exception str ) {}
    return ret;
  }
}

Comments
EVALUATION The code in question currently fails to compile with the following error: negtest06.java:7: cannot find symbol } catch( Exception str) { Object o = str2; } ^ symbol: variable str2 location: class negtest06 1 error That is correct according to the specification since the scope of the resource variables is not defined to extend to the catch blocks. The current implementation correctly rejects code like where the stated conditions are met: try(AutoCloseable c = new Test6980056()) { try { ; } catch(Exception c) {} } In contrast, try { ... } catch(RuntimeException e){...} catch(Exception e) {...} is legal today and analagous code is legal under try-with-resources. Closing as not a bug.
02-09-2010

EVALUATION Since resource variables cannot be referenced from the catch block, I believe the current spec draft is overly restrictive - e.g. the following doesn't compile: class negtest06 { String test1( ) { String ret = null; try { try ( java.io.FileInputStream str2 = new java.io.FileInputStream("foo.txt") ) { ret = str2.getClass().getSimpleName(); } catch( Exception str) { Object o = str2; } } catch ( Exception str ) {} return ret; } } [and I remember in an early version of the compiler this used to work but failed with VerifyError at runtime]. Reassigning for further evaluation.
26-08-2010