1 Reply Latest reply on Dec 12, 2013 2:06 AM by jaikiran pai

    try catch block does not catch an exception

    Matti Linnanvuori Newbie

      A try catch block does not catch an exception in a bean. An exception is thrown in method deleteMetaRoute but its try catch block does not catch it.

       

      @DELETE
      @Path("/{metaRouteId}")
      public StatusElement deleteMetaRoute(
              @PathParam("metaRouteId") int metaRouteId,
              @Context HttpServletRequest req) throws Exception;

       

      @Override
      public StatusElement deleteMetaRoute(final int metaRouteId,
              final HttpServletRequest req) {
          final String METHOD = "deleteMetaRoute: ";

       

          final StatusElement status = new StatusElement();

       

          try {
              backend.deleteMetaRoute(metaRouteId);
              status.addSuccess(formSuccess(req, Integer.toString(metaRouteId)));
          } catch (final Exception e) {
              log.debug(METHOD + "error deleting a MetaRoute", e);
              status.addError(formErrorElement(Integer.toString(metaRouteId), e));
          }

       

          return status;
      }

       

       

      2013-12-11 01:51:22,612 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-59:REST-/rest/meta_route/2960084) SQL Error: 1451, SQLState: 23000

      2013-12-11 01:51:22,613 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-59:REST-/rest/meta_route/2960084) Cannot delete or update a parent row: a foreign key constraint fails (`production`.`k3_leg`, CONSTRAINT `k3_leg_startpoint_fk_fk` FOREIGN KEY (`startpoint_fk`) REFERENCES `k3_stop` (`id`))

      2013-12-11 01:51:22,613 WARN  [com.arjuna.ats.arjuna] (default task-59:REST-/rest/meta_route/2960084) ARJUNA012125: TwoPhaseCoordinator.beforeCompletion - failed for SynchronizationImple< 0:ffff90cae62c:ccba294:52a809c0:63cd2, org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization@7f4a3db0 >: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement

              at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1661) [hibernate-entitymanager-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1575) [hibernate-entitymanager-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1581) [hibernate-entitymanager-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.jpa.spi.AbstractEntityManagerImpl$CallbackExceptionMapperImpl.mapManagedFlushFailure(AbstractEntityManagerImpl.java:1780) [hibernate-entitymanager-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:118) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:53) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:76)

          at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:273)
          at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:93)
          at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:162)
          at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1170)
          at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:126)
          at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:75)
          at org.jboss.as.ejb3.tx.CMTTxInterceptor.endTransaction(CMTTxInterceptor.java:93) [wildfly-ejb3-8.0.0.Beta1.jar:8.0.0.Beta1]
          at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:277) [wildfly-ejb3-8.0.0.Beta1.jar:8.0.0.Beta1]
          at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:340) [wildfly-ejb3-8.0.0.Beta1.jar:8.0.0.Beta1]
          at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:239) [wildfly-ejb3-8.0.0.Beta1.jar:8.0.0.Beta1]
          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)
          at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) [wildfly-

      ejb3-8.0.0.Beta1.jar:8.0.0.Beta1]

              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

              at org.jboss.as.ejb3.component.invocationmetrics.WaitTimeInterceptor.processInvocation(WaitTimeInterceptor.java:43) [wildfly-ejb3-8.0.0.Beta1.jar:8.0.0.Beta1]

              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

              at org.jboss.as.ejb3.security.SecurityContextInterceptor.processInvocation(SecurityContextInterceptor.java:95) [wildfly-ejb3-8.0.0.Beta1.jar:8.0.0.Beta1]

              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

              at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.java:64) [wildfly-ejb3-8.0.0.Beta1.jar:8.0.0.Beta1]

              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

              at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59) [wildfly-ejb3-8.0.0.Beta1.jar:8.0.0.Beta1]

              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

              at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)

              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

              at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:55) [wildfly-ejb3-8.0.0.Beta1.jar:8.0.0.Beta1]

              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

              at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:64)

              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

              at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:325)

              at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:437)

              at org.jboss.invocation.AccessCheckingInterceptor.processInvocation(AccessCheckingInterceptor.java:61)

              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

              at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:325)

              at org.jboss.invocation.PrivilegedWithCombinerInterceptor.processInvocation(PrivilegedWithCombinerInterceptor.java:80)

              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

              at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)

              at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:165)

              at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:182)

              at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

              at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)

              at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:72)

              at com.ecolane.mpk.rest.interfaces.MetaRouteRestLocal$$$view81.deleteMetaRoute(Unknown Source) [k3-main.jar:]

              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_03]

              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_03]

              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_03]

       

          at java.lang.reflect.Method.invoke(Method.java:601) [rt.jar:1.7.0_03]
          at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137) [resteasy-jaxrs-3.0.4.Final.jar:]
          at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:280) [resteasy-jaxrs-3.0.4.Final.jar:]
          at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:234) [resteasy-jaxrs-3.0.4.Final.jar:]
          at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:221) [resteasy-jaxrs-3.0.4.Final.jar:]
          at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356) [resteasy-jaxrs-3.0.4.Final.jar:]
          at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179) [resteasy-jaxrs-3.0.4.Final.jar:]
          at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) [resteasy-jaxrs-3.0.4.Final.jar:]
          at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) [resteasy-jaxrs-3.0.4.Final.jar:]
          at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) [resteasy-jaxrs-3.0.4.Final.jar:]
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [jboss-servlet-api_3.1_spec-1.0.0.Beta1.jar:1.0.0.Beta1]
          at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:87)

       

       

       

       

          at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:59)
          at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
          at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:81)
          at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
          at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113)
          at io.undertow.security.handlers.AuthenticationCallHandler.handleRequest(AuthenticationCallHandler.java:52)
          at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
          at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:65)
          at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:70)
          at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
          at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
          at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest

      (ServletInitialHandler.java:218)

              at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:205)

              at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:69)

              at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:134)

              at io.undertow.server.Connectors.executeRootHandler(Connectors.java:138)

              at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:622)

              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_03]

              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_03]

              at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_03]

      Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement

              at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:74) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlException

      Helper.java:126) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:143) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:60) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3346) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3549) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:113) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:377) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:369) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:293) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:340) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(Defaul

      tFlushEventListener.java:52) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1235) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:405) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorImpl.beforeCompletion(SynchronizationCallbackCoordinatorImpl.java:113) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              ... 78 more

      Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`production`.`k3_leg`, CONSTRAINT `k3_leg_startpoint_fk_fk` FOREIGN KEY (`startpoint_fk`) REFERENCES `k3_stop` (`id`))

              at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [rt.jar:1.7.0_03]

              at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) [rt.jar:1.7.0_03]

              at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) [rt.jar:1.7.0_03]

              at java.lang.reflect.Constructor.newInstance(Constructor.java:525) [rt.jar:1.7.0_03]

              at com.mysql.jdbc.Util.handleNewInstance(Util.java:411) [mysql-connector-java-5.1.26-bin.jar:]

       

          at com.mysql.jdbc.Util.getInstance(Util.java:386) [mysql-connector-java-5.1.26-bin.jar:]
          at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1041) [mysql-connector-java-5.1.26-bin.jar:]
          at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4190) [mysql-connector-java-5.1.26-bin.jar:]
          at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4122) [mysql-connector-java-5.1.26-bin.jar:]
          at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2570) [mysql-connector-java-5.1.26-bin.jar:]
          at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2731) [mysql-connector-java-5.1.26-bin.jar:]
          at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2818) [mysql-connector-java-5.1.26-bin.jar:]
          at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2157) [mysql-connector-java-5.1.26-bin.jar:]
          at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2460) [mysql-connector-java-5.1.26-bin.jar:]
          at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2377) [mysql-connector-java-5.1.26-bin.jar:]
          at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2361) [mysql-connector-java-5.1.26-bin.jar:]
          at com.mysql.jdbc.jdbc2.optional.PreparedStatementWrapper.executeUpdate(

      PreparedStatementWrapper.java:875) [mysql-connector-java-5.1.26-bin.jar:]

              at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeUpdate(WrappedPreparedStatement.java:493)

              at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:140) [hibernate-core-4.3.0.Beta4.jar:4.3.0.Beta4]

              ... 90 more

       

       

       

       

        • 1. Re: try catch block does not catch an exception
          jaikiran pai Master

          This is typical where the transaction commit (or transaction callbacks) happen after the your method execution is complete. So if you have a method with container managed semantics, the transaction actually gets committed after the code in your method is completed and any errors in that commit part will obviously not be caught by your try/catch block since the execution is already past that point.