在做测试的时候,在添加了唯一性约束的表中,插入重复数据,数据库报“ORA-00001: 违反唯一约束条件 (oracle用户名.oracle约束名称)”,原本想在action层获取异常信息的,通过JDBCException和HibernateException都无法捕获到异常,通过RuntimeException和Exception才可以捕获到异常,但是捕获到的异常通过e.getLocalizedMessage()调用,只有显示“Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update”,却没有数据库底层的异常。或通过查询文档发现,如果要获取底层的异常,需要通过e.getCause()递归获取,直到e.getCause()==null为止,才可以获取到底层的异常。
如:
如:
- catch (RuntimeException e) {
- new DaoTest() {
- private void showTraces(Throwable t) {
- Throwable next = t.getCause();
- if (next == null) {
- System.out.println(t.getMessage());
- } else {
- showTraces(next);
- }
- }
- }.showTraces(e);
- }