If I have exception handling part in my code, and it can fire 4 sqlException, what whould be the best way to handle them which tells the most about what happend and where ?
I made a poor man solution (by using ‘exceptionDesc’).
Is there any good practice to handle this situation ? (Without making unique class and subclasses )
try {
exceptionDesc = "prepareStatement";
statement = connection.prepareStatement(sql);
exceptionDesc = "statement.set...";
statement.setDate(1, sqlDate);
exceptionDesc = "executeUpdate";
statement.executeUpdate();
exceptionDesc = "commit";
connection.commit();
}
catch (SQLException sqlEx) {
if (exceptionDesc.equals("prepareStatement")) {
LOG.error ...
} else if (exceptionDesc.equals("executeQuery")) {
LOG.error(" executeQuery ...
...
...
throw new SQLException(sqlEx);
}
Don’t do anything. The stack trace of the exception does that for you:
is sufficient. The logger, if configured to show stack traces (which it should do), will print the stack trace and the stack trace will tell exactly which method, at which line, threw the exception. It will also tell which method called the method causing the exception, etc. until the bottom of the stack.