7017104: improve error reporting for uncaught/undeclared exceptions from try-with-resources

Tue, 15 Feb 2011 11:51:04 +0000

author
mcimadamore
date
Tue, 15 Feb 2011 11:51:04 +0000
changeset 878
fa0e4e1916f4
parent 877
351027202f60
child 879
846d6644bb70

7017104: improve error reporting for uncaught/undeclared exceptions from try-with-resources
Summary: twr should generate better error message when uncaught exceptions are thrown by implicit call of close() method
Reviewed-by: jjg

src/share/classes/com/sun/tools/javac/comp/Flow.java file | annotate | diff | comparison | revisions
src/share/classes/com/sun/tools/javac/resources/compiler.properties file | annotate | diff | comparison | revisions
test/tools/javac/TryWithResources/ResourceInterface.out file | annotate | diff | comparison | revisions
test/tools/javac/TryWithResources/TwrFlow.out file | annotate | diff | comparison | revisions
test/tools/javac/diags/examples/UnreportedExceptionImplicitClose.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Flow.java	Tue Feb 15 11:49:46 2011 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Flow.java	Tue Feb 15 11:51:04 2011 +0000
     1.3 @@ -314,13 +314,22 @@
     1.4          for (PendingExit exit = pendingExits.next();
     1.5               exit != null;
     1.6               exit = pendingExits.next()) {
     1.7 -            boolean synthetic = classDef != null &&
     1.8 -                classDef.pos == exit.tree.pos;
     1.9 -            log.error(exit.tree.pos(),
    1.10 -                      synthetic
    1.11 -                      ? "unreported.exception.default.constructor"
    1.12 -                      : "unreported.exception.need.to.catch.or.throw",
    1.13 -                      exit.thrown);
    1.14 +            if (classDef != null &&
    1.15 +                classDef.pos == exit.tree.pos) {
    1.16 +                log.error(exit.tree.pos(),
    1.17 +                        "unreported.exception.default.constructor",
    1.18 +                        exit.thrown);
    1.19 +            } else if (exit.tree.getTag() == JCTree.VARDEF &&
    1.20 +                    ((JCVariableDecl)exit.tree).sym.isResourceVariable()) {
    1.21 +                log.error(exit.tree.pos(),
    1.22 +                        "unreported.exception.implicit.close",
    1.23 +                        exit.thrown,
    1.24 +                        ((JCVariableDecl)exit.tree).sym.name);
    1.25 +            } else {
    1.26 +                log.error(exit.tree.pos(),
    1.27 +                        "unreported.exception.need.to.catch.or.throw",
    1.28 +                        exit.thrown);
    1.29 +            }
    1.30          }
    1.31      }
    1.32  
    1.33 @@ -1021,7 +1030,7 @@
    1.34                              List.<Type>nil());
    1.35                      if (closeMethod.kind == MTH) {
    1.36                          for (Type t : ((MethodSymbol)closeMethod).getThrownTypes()) {
    1.37 -                            markThrown(tree.body, t);
    1.38 +                            markThrown(resource, t);
    1.39                          }
    1.40                      }
    1.41                  }
     2.1 --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Feb 15 11:49:46 2011 +0000
     2.2 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Feb 15 11:51:04 2011 +0000
     2.3 @@ -800,6 +800,11 @@
     2.4  compiler.err.unreported.exception.default.constructor=\
     2.5      unreported exception {0} in default constructor
     2.6  
     2.7 +# 0: type, 1: name
     2.8 +compiler.err.unreported.exception.implicit.close=\
     2.9 +    unreported exception {0}; must be caught or declared to be thrown\n\
    2.10 +    exception thrown from implicit call to close() on resource variable ''{1}''
    2.11 +
    2.12  compiler.err.unsupported.cross.fp.lit=\
    2.13      hexadecimal floating-point literals are not supported on this VM
    2.14  
     3.1 --- a/test/tools/javac/TryWithResources/ResourceInterface.out	Tue Feb 15 11:49:46 2011 +0000
     3.2 +++ b/test/tools/javac/TryWithResources/ResourceInterface.out	Tue Feb 15 11:51:04 2011 +0000
     3.3 @@ -1,2 +1,2 @@
     3.4 -ResourceInterface.java:38:34: compiler.err.unreported.exception.need.to.catch.or.throw: ResourceInterface.E1
     3.5 +ResourceInterface.java:38:13: compiler.err.unreported.exception.implicit.close: ResourceInterface.E1, r2
     3.6  1 error
     4.1 --- a/test/tools/javac/TryWithResources/TwrFlow.out	Tue Feb 15 11:49:46 2011 +0000
     4.2 +++ b/test/tools/javac/TryWithResources/TwrFlow.out	Tue Feb 15 11:51:04 2011 +0000
     4.3 @@ -1,3 +1,3 @@
     4.4  TwrFlow.java:14:11: compiler.err.except.never.thrown.in.try: java.io.IOException
     4.5 -TwrFlow.java:12:46: compiler.err.unreported.exception.need.to.catch.or.throw: CustomCloseException
     4.6 +TwrFlow.java:12:13: compiler.err.unreported.exception.implicit.close: CustomCloseException, twrFlow
     4.7  2 errors
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/tools/javac/diags/examples/UnreportedExceptionImplicitClose.java	Tue Feb 15 11:51:04 2011 +0000
     5.3 @@ -0,0 +1,33 @@
     5.4 +/*
     5.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
     5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     5.7 + *
     5.8 + * This code is free software; you can redistribute it and/or modify it
     5.9 + * under the terms of the GNU General Public License version 2 only, as
    5.10 + * published by the Free Software Foundation.
    5.11 + *
    5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    5.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    5.15 + * version 2 for more details (a copy is included in the LICENSE file that
    5.16 + * accompanied this code).
    5.17 + *
    5.18 + * You should have received a copy of the GNU General Public License version
    5.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    5.21 + *
    5.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    5.23 + * or visit www.oracle.com if you need additional information or have any
    5.24 + * questions.
    5.25 + */
    5.26 +
    5.27 +// key: compiler.err.unreported.exception.implicit.close
    5.28 +
    5.29 +class UnreportedExceptionImplicitClose {
    5.30 +    static class MyCloseable implements AutoCloseable {
    5.31 +        public void close() throws Exception { }
    5.32 +    }
    5.33 +    void test() {
    5.34 +        try (MyCloseable x = new MyCloseable()) {  }
    5.35 +    }
    5.36 +}

mercurial