如果我的代码中有异常处理部分,并且它可以触发 4 个 sqlException,那么处理它们的最佳方法是什么,它最能说明发生了什么以及在哪里?我做了一个穷人解决方案(通过使用'exceptionDesc')。
有什么好的做法来处理这种情况吗?(没有制作独特的类和子类)
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);
}