# HG changeset patch # User mcimadamore # Date 1297770664 0 # Node ID 1e9935b883cdc966d7f8ab175f957e7bacbe2932 # Parent 703181b017731faf3757ef949edc4fd59de7a18d 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 diff -r 703181b01773 -r 1e9935b883cd langtools/src/share/classes/com/sun/tools/javac/comp/Flow.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.nil()); if (closeMethod.kind == MTH) { for (Type t : ((MethodSymbol)closeMethod).getThrownTypes()) { - markThrown(tree.body, t); + markThrown(resource, t); } } } diff -r 703181b01773 -r 1e9935b883cd langtools/src/share/classes/com/sun/tools/javac/resources/compiler.properties --- 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 diff -r 703181b01773 -r 1e9935b883cd langtools/test/tools/javac/TryWithResources/ResourceInterface.out --- 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 diff -r 703181b01773 -r 1e9935b883cd langtools/test/tools/javac/TryWithResources/TwrFlow.out --- 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 diff -r 703181b01773 -r 1e9935b883cd langtools/test/tools/javac/diags/examples/UnreportedExceptionImplicitClose.java --- /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()) { } + } +}