Getting returned messages

In this topic

About getting returned messages

When tools are executed in a program, you may want to access the resulting messages if an error or warning occurs, or to show an operation's progress. The returned messages can be filtered with a severity option using the GetMessages method. The following code example shows how to print error messages:
// Print error messages.
String messages = gp.getMessages(2);


Individual messages can be retrieved using the GetMessage method. GetMessage has one parameter, which is the index of the message in the geoprocessor's message list or array. The MessageCount property maintains the number of messages in the geoprocessor's message array. Put this code in a catch block outside your Execute calls since this will iterate through all the messages for a failed execution. The following code example returns all the tool's messages after a catch:
// If there is an error, loop the messages returned by the geoprocessor
// to look for the error.
GPMessages gpMessages = (GPMessages)gp.getReturnMessages();
for (int i = 0; i gpMessages.getCount(); i++){


The preceding code example shows messages retrieved by using methods available on the geoprocessor. However, it is also possible to get the messages from the IGeoProcessorResult object. When executing geoprocessing server tools, retrieve the messages from IGeoProcessorResult. See the following code example:
// Execute the Union tool.
IGeoProcessorResult pResult = gp.execute(uniontool, null);
if (pResult.getMessageCount() > 0){
    for (int i = 0; i <  = pResult.getMessageCount() - 1; i++){