是否可以从 EJB 中的 IllegalArgumentException 检索 org.postgresql.util.PSQLException 的原因?
Is it possibile to retrieve org.postgresql.util.PSQLException's cause from IllegalArgumentException inside EJB?
我有一个使用 EJB3 的项目,当代码抛出与 SQL 相关的异常时,它会向开发团队发送通知。
因此,例如,使用此代码:
Query q = getEntityManager().createNativeQuery(query);
List<Object[]> results = q.getResultList();
如果我犯了语法错误,我会捕获第一个发送给开发人员的异常 java.lang.IllegalArgumentException: Parameter with that position [2] did not exist
。
在 Eclipse 中,我可以看到其他异常,例如 javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
,最后 最后一个包含我要捕获的消息的异常 :Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "AND"
。有没有办法获得更多详细信息?
我可以看到抑制的异常数组是空的。
更新:如果我在 catch
中直接插入 org.postgresql.util.PSQLException
,Eclipse 指出:
This exception is never thrown from the try statement body
您在 Eclipse stracktrace 中看到的异常是存储在 IllegalArgumentException
中的异常,形成了错误原因链。
您可以通过遍历此原因链来获取 postgres 异常:
catch(IllegalArgumentException e) {
Throwable t = e;
while (t != null)
{
if (t instanceof PSQLException) {
PSQLException pe = (PSQLException)t;
// analyse the exception
break;
}
t = t.getCause();
}
}
我有一个使用 EJB3 的项目,当代码抛出与 SQL 相关的异常时,它会向开发团队发送通知。 因此,例如,使用此代码:
Query q = getEntityManager().createNativeQuery(query);
List<Object[]> results = q.getResultList();
如果我犯了语法错误,我会捕获第一个发送给开发人员的异常 java.lang.IllegalArgumentException: Parameter with that position [2] did not exist
。
在 Eclipse 中,我可以看到其他异常,例如 javax.ejb.EJBException: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
,最后 最后一个包含我要捕获的消息的异常 :Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "AND"
。有没有办法获得更多详细信息?
我可以看到抑制的异常数组是空的。
更新:如果我在 catch
中直接插入 org.postgresql.util.PSQLException
,Eclipse 指出:
This exception is never thrown from the try statement body
您在 Eclipse stracktrace 中看到的异常是存储在 IllegalArgumentException
中的异常,形成了错误原因链。
您可以通过遍历此原因链来获取 postgres 异常:
catch(IllegalArgumentException e) {
Throwable t = e;
while (t != null)
{
if (t instanceof PSQLException) {
PSQLException pe = (PSQLException)t;
// analyse the exception
break;
}
t = t.getCause();
}
}