我无法理解为什么编译器在以下代码中警告我资源泄漏(资源泄漏:’conn’未在此位置关闭):
Connection conn = null;
try {
conn = DatabaseConnectionPool.getConnectionFromPool();
// Some code
try {
// Other code
} catch (final SomeException e) {
// More code
throw e; // Resource leak: 'conn' is not closed at this location
}
} catch (final SQLException | OtherExceptions e) {
// Some more code
} finally {
try {
// Another bunch of code
} finally {
DatabaseConnectionPool.freeConnection(conn);
}
}
请注意,如果我这样写
Connection conn = null;
try {
conn = DatabaseConnectionPool.getConnectionFromPool();
// Some code
try {
// Other code
} catch (final SomeException e) {
// More code
throw e;
} finally {
DatabaseConnectionPool.freeConnection(conn);
}
} catch (final SQLException | OtherExceptions e) {
// Some more code
} finally {
// Another bunch of code
}
警告消失了.