Handling errors

 

Methods in the Java™ interface do not return a completion code and reason code. Instead, they throw an exception whenever the completion code and reason code resulting from a WebSphere MQ call are not both zero. This simplifies the program logic so that you do not have to check the return codes after each call to WebSphere MQ. We can decide at which points in your program you want to deal with the possibility of failure. At these points, we can surround your code with try and catch blocks, as in the following example:

try {
    myQueue.put(messageA,putMessageOptionsA);
    myQueue.put(messageB,putMessageOptionsB);
}
catch (MQException ex) {
    // This block of code is only executed if one of
    // the two put methods gave rise to a non-zero
    // completion code or reason code.
    System.out.println("An error occurred during the put operation:" +
                          "CC = " + ex.completionCode +
                          "RC = " + ex.reasonCode);
    System.out.println("Cause exception:" + ex.getCause() );
}

The WebSphere MQ call reason codes reported back in Java exceptions are documented in a chapter called "Return Codes" in the WebSphere MQ Application Programming Reference.

Exceptions that are thrown while a WebSphere MQ base Java application is running are also written to the log. However, an application can call the MQException.logExclude() method to prevent exceptions associated with a specific reason code from being logged. You might want to do this in situations where you expect many exceptions associated with a specific reason code to be thrown, and you do not want the log to be filled with these exceptions. For example, if your application attempts to get a message from a queue each time it iterates around a loop and, for most of these attempts, you expect no suitable message to be on the queue, you might want to prevent exceptions associated the reason code MQRC_NO_MSG_AVAILABLE from being logged. If an application has previously prevented exceptions associated with a specific reason code from being logged, it can allow these exceptions to be logged again by calling the method MQException.logInclude().

Sometimes the reason code does not convey all details associated with the error. This can occur if WebSphere MQ uses services provided by another product (for example, a JSSE implementation) that throws a java.lang.Exception to WebSphere MQ Java. In this case, the method MQException.getCause() retrieves the underlying java.lang.Exception that caused the error.


uj11140_