I saw lots of questions and explanations regarding empty catch clauses but I was never actually sure what "empty" means.
Is a catch clause like this empty? It clearly isn't empty for the eye because there is one line of code. But the catch is not really handled. Instead the error is just logged.
try {
// do some work
} catch (SomeException e) {
logger.error("Some error", e);
}
I see this fairly often because there are a lot exceptions where you just can't do something about it if it fails. The consequence is that the error is not fully swallowed but at least logged.
So the question is: Is this bad code if you "just" log the error and don't do anything about?
catch
, it should be commented. That's different from "I don't expect this ever to happen". And sadly there are cases where you have to have a catch block even though you know it can't throw (using "UTF-8" in the string constructor is a classic case: the JVM is required to support that, so it can't throw anUnsupportedEncodingException
). – Joachim Sauer Jul 22 '13 at 10:42