commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rand McNeely" <rand_mcne...@yahoo.com>
Subject [lang] New feature: Close method for Objects
Date Sun, 30 Jun 2002 20:16:15 GMT
I've attached a close method for Objects.  It uses reflection to call
close on the object passed to it and returns any exceptions (without
throwing them).  I have found this to be very useful as most classes
which have a close method declare to throw an exception that can't be
dealt with in a meaningful way (java.io.InputStream,
java.sql.Connection, etc).  This leads to constructs like this:

finally {
	try {
		if (statement != null) {
			statement.close();
		}
	} catch (SQLException ignored) {
	}
	try {
		if (connection != null) {
			connection.close();
		}
	} catch (SQLException ignored) { }
	}
}

which would be much cleaner like this:

finally {
	Objects.close(statement);
	Objects.close(connection);
}

Rand McNeely

Mime
View raw message