# Set an Exception Listener

All the APIs support the ability to set an exception listener on the connection that gets invoked when a connection breaks or experiences a fault-tolerant failover.

When the event is a disconnect, the exception handler can call various EMS methods without any problem. However, when the event is a fault-tolerant failover, the exception handler is not allowed to call any EMS method. To do so risks a deadlock. You can call the `setExceptionOnFTSwitch` method to receive an exception that contains the new server URL after a fault-tolerant failover has occurred.

The following examples demonstrate how to establish an exception listener for a connection.

- Java

  Implement an `ExceptionListener.onException` method, use the `Connection` object’s `setExceptionListener` method to register the exception listener, and call `Tibjms.setExceptionOnFTSwitch` to call the exception handler after a fault-tolerant failover:

        public class tibjmsMsgConsumer
                implements ExceptionListener
        {
      .....
        public void onException(JMSException e)
             {
               /* Handle exception */
             }
      .....
        connection.setExceptionListener(this);
        com.tibco.tibjms.Tibjms.setExceptionOnFTSwitch(true);
      .....
        }

  See the `tibjmsMsgConsumer.java` sample client for a working example (without the `setExceptionOnFTSwitch` call).

- C

  Define an `onException` function to handle exceptions, use the `tibemsConnection_SetExceptionListener` function to call `onException` when an error is encountered, and call `tibems_setExceptionOnFTSwitch` to call the exception handler after a fault-tolerant failover:

        void onException(
          tibemsConnection    conn,
          tibems_status       reason,
          void*               closure)
        {
           /* Handle exception */
        }
      .....
        status = tibemsConnection_SetExceptionListener(
                                                  connection,
                                                  onException,
                                                  NULL);
        tibems_setExceptionOnFTSwitch(TIBEMS_TRUE);

  See the `tibemsMsgConsumer.c` sample client for a working example (without the `setExceptionOnFTSwitch` call).

- C#

  Implement an `IExceptionListener.OnException` method, set the `Connection` object’s `ExceptionListener` property to register the exception listener, and call `Tibems.SetExceptionOnFTSwitch` to call the exception handler after a fault-tolerant failover:

        public class csMsgConsumer : IExceptionListener
        {
      .....
        public void OnException(EMSException e)
            {
           /* Handle exception */
            }
      .....
        connection.ExceptionListener = this;
        TIBCO.EMS.Tibems.SetExceptionOnFTSwitch(true);
      .....
        }

  See the `csMsgConsumer.cs` sample client for a working example (without the `setExceptionOnFTSwitch` call).

Copyright © Cloud Software Group, Inc. All rights reserved.