尽管有例外,但仍然执行了声明

时间:2014-03-13 15:33:28

标签: java exception sqlexception

我有这个非常简单的代码片段,如果没有抛出异常,它会按预期运行:

try {
  session = HibernateHelper.getSessionFactory().openSession();
  session.beginTransaction();   
  OracleTables.writeToLogTable(thisLogRecord); // <<<< this throws a java.sql.SQLException!


  boolean isSuccess = OracleTables.writeToActualDataTable(thisDataRecord);
  if (isSuccess) {
    System.out.println("writeToActualDataTable() successful.");
  }
}
catch (RuntimeException e) {
  if (e != null && e.getMessage() != null && e.getMessage().contains("No data to write")) {
    System.out.println("No more data to write.");
    System.exit(0);
  }
  else
    throw e;
}

但是如果在第一次数据库写入中存在一些约束违规(例如尝试将NULL插入到不允许它的字段中),则抛出java.sql.SQLException(这很好)但是对于一些奇怪的原因,OracleTables.writeToActualDataTable()语句正在执行!

org.hibernate.exception.ConstraintViolationException: could not execute statement
        at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129)
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:136)
        at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:58)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3067)
        at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3509)
        at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
        at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377)
        at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369)
        at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:286)
        at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:339)
        at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
        at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1234)
        at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:404)
        at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
        at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
        at com.nicecor.test.myclient.OracleTables.writeToLogTable(OracleTables.java:159)
        at com.nicecor.test.myws.ServicePort_ServiceSoapPort_Client.main(ServicePort_ServiceSoapPort_Client.java:180)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at com.simontuffs.onejar.Boot.run(Boot.java:340)
        at com.simontuffs.onejar.Boot.main(Boot.java:166)
Caused by: java.sql.SQLException: ORA-01400: cannot insert NULL into ("USR"."LOG_TABLE"."CREATE_TMSTMP")

这很奇怪。我认为Java中的异常将中止执行并立即转到catch子句。

什么可以解释这种奇怪的行为?

我错过了什么?

1 个答案:

答案 0 :(得分:1)

OracleTables.writeToLogTable()正在捕获异常并在继续执行时向System.err打印内容。