7017104: improve error reporting for uncaught/undeclared exceptions from try-with-resources
authormcimadamore
Tue, 15 Feb 2011 11:51:04 +0000
changeset 8428 1e9935b883cd
parent 8427 703181b01773
child 8429 510df4cd65d7
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
langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java
langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties
langtools/test/tools/javac/TryWithResources/ResourceInterface.out
langtools/test/tools/javac/TryWithResources/TwrFlow.out
langtools/test/tools/javac/diags/examples/UnreportedExceptionImplicitClose.java
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java	Tue Feb 15 11:49:46 2011 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/Flow.java	Tue Feb 15 11:51:04 2011 +0000
@@ -314,13 +314,22 @@
         for (PendingExit exit = pendingExits.next();
              exit != null;
              exit = pendingExits.next()) {
-            boolean synthetic = classDef != null &&
-                classDef.pos == exit.tree.pos;
-            log.error(exit.tree.pos(),
-                      synthetic
-                      ? "unreported.exception.default.constructor"
-                      : "unreported.exception.need.to.catch.or.throw",
-                      exit.thrown);
+            if (classDef != null &&
+                classDef.pos == exit.tree.pos) {
+                log.error(exit.tree.pos(),
+                        "unreported.exception.default.constructor",
+                        exit.thrown);
+            } else if (exit.tree.getTag() == JCTree.VARDEF &&
+                    ((JCVariableDecl)exit.tree).sym.isResourceVariable()) {
+                log.error(exit.tree.pos(),
+                        "unreported.exception.implicit.close",
+                        exit.thrown,
+                        ((JCVariableDecl)exit.tree).sym.name);
+            } else {
+                log.error(exit.tree.pos(),
+                        "unreported.exception.need.to.catch.or.throw",
+                        exit.thrown);
+            }
         }
     }
 
@@ -1021,7 +1030,7 @@
                             List.<Type>nil());
                     if (closeMethod.kind == MTH) {
                         for (Type t : ((MethodSymbol)closeMethod).getThrownTypes()) {
-                            markThrown(tree.body, t);
+                            markThrown(resource, t);
                         }
                     }
                 }
--- a/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Feb 15 11:49:46 2011 +0000
+++ b/langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties	Tue Feb 15 11:51:04 2011 +0000
@@ -800,6 +800,11 @@
 compiler.err.unreported.exception.default.constructor=\
     unreported exception {0} in default constructor
 
+# 0: type, 1: name
+compiler.err.unreported.exception.implicit.close=\
+    unreported exception {0}; must be caught or declared to be thrown\n\
+    exception thrown from implicit call to close() on resource variable ''{1}''
+
 compiler.err.unsupported.cross.fp.lit=\
     hexadecimal floating-point literals are not supported on this VM
 
--- a/langtools/test/tools/javac/TryWithResources/ResourceInterface.out	Tue Feb 15 11:49:46 2011 +0000
+++ b/langtools/test/tools/javac/TryWithResources/ResourceInterface.out	Tue Feb 15 11:51:04 2011 +0000
@@ -1,2 +1,2 @@
-ResourceInterface.java:38:34: compiler.err.unreported.exception.need.to.catch.or.throw: ResourceInterface.E1
+ResourceInterface.java:38:13: compiler.err.unreported.exception.implicit.close: ResourceInterface.E1, r2
 1 error
--- a/langtools/test/tools/javac/TryWithResources/TwrFlow.out	Tue Feb 15 11:49:46 2011 +0000
+++ b/langtools/test/tools/javac/TryWithResources/TwrFlow.out	Tue Feb 15 11:51:04 2011 +0000
@@ -1,3 +1,3 @@
 TwrFlow.java:14:11: compiler.err.except.never.thrown.in.try: java.io.IOException
-TwrFlow.java:12:46: compiler.err.unreported.exception.need.to.catch.or.throw: CustomCloseException
+TwrFlow.java:12:13: compiler.err.unreported.exception.implicit.close: CustomCloseException, twrFlow
 2 errors
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/diags/examples/UnreportedExceptionImplicitClose.java	Tue Feb 15 11:51:04 2011 +0000
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+// key: compiler.err.unreported.exception.implicit.close
+
+class UnreportedExceptionImplicitClose {
+    static class MyCloseable implements AutoCloseable {
+        public void close() throws Exception { }
+    }
+    void test() {
+        try (MyCloseable x = new MyCloseable()) {  }
+    }
+}