提交 6e24a388 编写于 作者: M mcimadamore

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
上级 91420acf
......@@ -314,13 +314,22 @@ public class Flow extends TreeScanner {
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 @@ public class Flow extends TreeScanner {
List.<Type>nil());
if (closeMethod.kind == MTH) {
for (Type t : ((MethodSymbol)closeMethod).getThrownTypes()) {
markThrown(tree.body, t);
markThrown(resource, t);
}
}
}
......
......@@ -800,6 +800,11 @@ compiler.err.unreported.exception.need.to.catch.or.throw=\
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
......
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
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
/*
* 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()) { }
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册