Connection.close() may throw SqlException, but I have always assumed that it is safe to ignore any such exceptions (and I have never seen code that does not ignore them).
Normally I would write:
try{ connection.close(); }catch(Exception e) {}
Or
try{ connection.close(); }catch(Exception e) { logger.log(e.getMessage(), e); }
The question is:
- Is it bad practice (and has anyone had problems when ignoring such exceptions).
- When
Connection.close()does throw any exception. - If it is bad how should I handle the exception.
Comment:
I know that discarding exceptions is evil, but I’m reffering only to exceptions thrown when closing a connection (and as I’ve seen this is fairly common in this case).
Does anyone know when Connection.close() may throw anything?
Actually, what you’re doing is (almost) best practice 🙂 here’s what I saw in Spring’s JdbcUtils.java. So, you might want to add another Catch block.