我的代码有一个DataAccessException
,但无法捕获,为什么?
CustomerPlan newCust = data.customerPlan;
try {
this.demandService.doCreateCustomerPlanAndDemandForecast(newCust,
newDf);
} catch (DataAccessException de) {
log.info("de ------------------------------------");
de.printStackTrace();
log.info("de end ------------------------");
} catch (Exception le) {
log.info("de ------------------------------------");
le.printStackTrace();
log.info("de end ------------------------");
} catch (Throwable t) {
log.info("tttttttttttttttttttttttt");
t.printStackTrace();
}
像这样的异常:
at rmes.service.demand.DemandService.doCreateCustomerPlanAndDemandForecast(DemandService.java:132)
<小时/>
@Override
public Serializable save(final Object entity) throws DataAccessException {
return executeWithNativeSession(new HibernateCallback<Serializable>() {
@Override
public Serializable doInHibernate(Session session) throws HibernateException {
checkWriteOperationAllowed(session);
return session.save(entity);
}
});
}
这里出现异常,如何捕获?
最佳答案
My code has a
DataAccessException
, but it couldn't be caught, Why?
一些可能的解释是:
因为异常没有在 try/catch 中抛出;即它被扔到其他地方了。 (仔细阅读完整堆栈跟踪将告诉您这是否属实。)
因为有多个名为
DataAccessException
的异常(在不同的包中),并且您试图捕获错误的异常。 (仔细阅读完整堆栈跟踪和您的代码将告诉您这是否属实。)因为您以某种方式设法多次加载
DataAccessException
。 (只有当您的应用程序或框架在不同的上下文中使用不同的类加载器链时,才会发生这种情况;例如,如果您有多个 Web 应用程序,典型的 Web 容器就会执行此操作。)
您这样评论:
there is no catch anywhere except this.
这可能就是问题所在。如果异常不是在 this try/catch 的上下文中抛出的,那么 this try/catch 将无法捕获它!但是,除非您发布未捕获的异常的完整堆栈跟踪,否则我们将无法帮助您。
关于java - DataAccessException 无法捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27467285/