If some code between the creation of the resource and try statement fails, the resource doesn't get closed.
In this testcase the exception is explicit:
<test-code>
<description><![CDATA[Code betwen declaration and try, should fail]]></description>
<rule-property name="types">Connection</rule-property>
<expected-problems>1</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
String test = "";
Connection c = pool.getConnection();
if (test != null) {
throw new RuntimeException("haha"); // <- RuntimeException, Connection c is not closed
}
try {
} catch (Exception e) {
} finally {
c.close();
}
}
}
]]></code>
</test-code>
The previous code should be written:
Option 1:
Option 2
Diff: