I was wondering if it is the proper way to always put the return statement of a function with a try-catch
clause in the finally
clause.
I have for example this function which returns the version of an sql server:
public String getVersion() {
String version = new String();
final String query = "Select VERSION()";
Connection con = null;
Statement stmt = null;
try {
Helper.println("Getting driver...");
Class.forName(_driver);
Helper.println("Connecting to database...");
con = DriverManager.getConnection(_url, _user._username, _user._password);
stmt = con.createStatement();
Helper.println("Executing query...");
ResultSet rs = stmt.executeQuery(query);
rs.next();
version = rs.getString(1);
} catch (SQLException se) {
Helper.printErrln("SQL Error: " + se.getErrorCode() + ' ' + se.getMessage());
} catch (ClassNotFoundException e) {
Helper.printErrln("Driver Error: " + e.getMessage());
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se) {
}
try {
if (con != null) {
con.close();
}
} catch (SQLException se) {
}
return version;
}
}
I return the version variable always in the finally
clause in situations like these and it particularly convenient when I need to return a list because if something went wrong then I will just be left with an empty list which I prefer. For example:
public List<String> getDatabases() {
List<String> databases = new ArrayList<>();
Connection con = null;
Statement stmt = null;
try {
Helper.println("Getting driver...");
Class.forName(_driver);
Helper.println("Connecting to database...");
con = DriverManager.getConnection(_url, _user._username, _user._password);
stmt = con.createStatement();
Helper.println("Finding Databases...");
ResultSet rs = con.getMetaData().getCatalogs();
while (rs.next()) {
databases.add(rs.getString("TABLE_CAT"));
}
} catch (SQLException se) {
Helper.printErrln("SQL Error: " + se.getErrorCode() + " " + se.getMessage());
} catch (ClassNotFoundException e) {
Helper.printErrln("Driver Error: " + e.getMessage());
} finally {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException se) {
}// do nothing
try {
if (con != null) {
con.close();
}
} catch (SQLException se) {
}
return databases;
}
}
So first of all my question is: Is what I am doing proper or can it cause problems that I have not anticipated? Also what if I wanted to encapsulate the function and have it trow the exception to the parent class for handling, how would the return be handled then?
if (!version.trim().isEmpty()) System.out.println(version);
. So I can just add an else in there if I want to handle it. \$\endgroup\$