Handling errors

 

Any runtime errors in a JMS application are reported by exceptions. The majority of JMS methods throw a JMSException to indicate an error. It is good programming practice to catch these exceptions and display them on a suitable output device.

Each JMS exception encapsulates a message identifier and some associated message text, which describes the error that has occurred. The message identifier has the format MQJMSnnnn, where nnnn is an integer in the range 0000 to 9999. For each message identifier, there is a corresponding field in the MQJMS_Messages class. In the definition of the MQJMS_Messages class, the description of the field contains an explanation of why the error occurred and a suggested user response.

To determine which field in the MQJMS_Messages class corresponds to a given message identifier, look in the specification of the WebSphere MQ JMS API that is supplied as HTML pages generated by the Javadoc tool. The MQJMS_Messages section of the Constant Field Values page contains a table that lists each message identifier and its corresponding field in the MQJMS_Messages class.

A JMSException can contain a further exception embedded within it. For JMS, this can be a valuable way to pass important information about the error from the underlying transport. In the case of WebSphere MQ JMS, an MQException is thrown in WebSphere MQ base Java whenever an error occurs in WebSphere MQ, and this exception is usually included as the embedded exception in a JMSException.

The implementation of JMSException does not include the embedded exception in the output of its toString() method. Therefore, check explicitly for an embedded exception and print it out, as shown in the following example:

try {
  .
  . code that might throw a JMSException
  .
} catch (JMSException je) {
  System.err.println("caught "+je);
  Exception e = je.getLinkedException();
  if (e != null) {
    System.err.println("linked exception: "+e);
  }
}


uj25270_