Skip Headers

# Using Java

Previous

Next

Example: Implementing an Exception Handler shows an example exception handler implementation. In this implementation, the exception handler always tries to reestablish the connection if it has been reset by peer, but only retries a query if it is an instance of `ReadQuery`. Note that this exception handler either returns the result of the reexecuted `ReadQuery` or throws an exception.

Implementing an Exception Handler

    session.setExceptionHandler(
      new ExceptionHandler()
      {
        public Object handleException(RuntimeException exception)
        {
          if (exception instanceof DatabaseException)
          {
            DatabaseException dbex = (DatabaseException) exception;
            if ((dbex.getInternalException() instanceof SQLException) &&
               (((SQLException) dbex.getInternalException()).getErrorCode() == MyDriver.CONNECTION_RESET_BY_PEER))
            {
               dbex.getAccessor().reestablishConnection(dbex.getSession());
               if (dbex.getQuery() instanceof ReadQuery)
               {
                 return dbex.getSession().executeQuery(dbex.getQuery());
               }
               throw exception;
            }
          }
          throw exception;
        }
      }
    );

Note: Unhandled exceptions must be rethrown by the exception handler code.