--- old/src/share/classes/com/sun/tools/javac/comp/Lower.java 2011-02-16 17:26:18.000000000 -0800 +++ new/src/share/classes/com/sun/tools/javac/comp/Lower.java 2011-02-16 17:26:18.000000000 -0800 @@ -1525,20 +1525,25 @@ List catchClauses = List.of(make.Catch(catchExceptionDecl, catchBlock)); JCTry tryTree = make.Try(tryBlock, catchClauses, null); - // if (resource != null) resourceClose; - JCExpression nullCheck = makeBinary(JCTree.NE, - make.Ident(primaryException), - makeNull()); - JCIf closeIfStatement = make.If(nullCheck, + // if (primaryException != null) {try...} else resourceClose; + JCIf closeIfStatement = make.If(makeNonNullCheck(make.Ident(primaryException)), tryTree, makeResourceCloseInvocation(resource)); return make.Block(0L, List.of(closeIfStatement)); } private JCStatement makeResourceCloseInvocation(JCExpression resource) { - // create resource.close() method invocation - JCExpression resourceClose = makeCall(resource, names.close, List.nil()); - return make.Exec(resourceClose); + // create resource.close() method invocation protected by a null-check + + JCExpression resourceClose = makeCall(resource, + names.close, + List.nil()); + + return make.If(makeNonNullCheck(resource), make.Exec(resourceClose), null); + } + + private JCExpression makeNonNullCheck(JCExpression expression) { + return makeBinary(JCTree.NE, expression, makeNull()); } /** Construct a tree that represents the outer instance