Handling errors

Handle errors arising from IBM MQ classes for .NET using try and catch blocks.

Methods in the .NET interface do not return a completion code and reason code. Instead, they throw an exception whenever the completion code and reason code resulting from an IBM MQ call are not both zero. This simplifies the program logic so that we do not have to check the return codes after each call to IBM 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.
  Console.WriteLine("An error occurred during the put operation:" +
                        "CC = " + ex.CompletionCode +
                        "RC = " + ex.ReasonCode);
  Console.WriteLine("Cause exception:" + ex );
}