【发布时间】:2019-06-16 19:43:12
【问题描述】:
由于其复杂性,使用 Hibernate JPA 在 Oracle DB 中执行本机查询,
我想捕捉从SqlExceptionHelper 类抛出的" ORA-01722: Nombre non valide " 之类的异常,但捕捉到的是:
类 javax.persistence.PersistenceException: 无法提取 结果集
记录器错误跟踪我但未捕获:
jdbc.spi.SqlExceptionHelper : ORA-01722: Nombre non valide
BigDecimal customerId = null;
try {
Query q = entityManager.createNativeQuery(
"select acc.account_id as customerId from Account ...");
customerId = (BigDecimal) q.getSingleResult();
} catch (Exception e) {
logger.info("CLASS : " + e.getClass());
if (e instanceof PersistenceException) { // should display ORA-01722: Nombre non valide ?
logger.info("ERRROR : " + e.getMessage());
throw new SQLException(e.getMessage());
}else
if (e instanceof SQLException) {
logger.info("ERRROR : " + e.getMessage());
throw new SQLException(e.getMessage());
}
logger.info("NOOOOOOOOOOOOO : " + e.getMessage());
throw new Exception(e.getMessage());
}
【问题讨论】:
-
您不需要将其作为通用异常捕获。做检查实例是不必要的。您可以在不同的 catch 块中捕获不同的异常。
-
我同意,但我想向前面的用户显示这个异常 [ORA-01722: Nombre non valide] 让他知道他的问题!
-
stackoverflow.com/a/9257574/4993989 - 查看此答案。