cancel
Showing results for 
Search instead for 
Did you mean: 

HCI groovy try-catch not working

digirolamocristian60
Participant
0 Kudos
1,823

Hi experts,

I cannot catch the exception within a groovy script. I wrappep all the code up into a try-catch statement but the message still fails (red envelope in the message monitoring). After the catch statement I put "return message".

Have you ever experienced that? What could it be?

Thanks.

Cristian

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member666312
Active Participant
0 Kudos

Hi ,

In CPI try catch works fine, i have observed no issues. Please check the below code.

to execute the below create a header with the value try , if you give length of try more than 10 , MPL goes to completed. else fail as per the try catch.


import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
    //Body 
        def messageLog = messageLogFactory.getMessageLog(message);
       def body = message.getBody();
       def map = message.getHeaders();
       def head = map.get("head");
       
         messageLog.setLongProperty("Logging#1", head.length())
       
       try
       {
           if(head.length() > 10)
           {
               message.setHeader("YOU_ARE_RIGHT" , head);
               
               
           }
           else
           throw new RuntimeException("head length is less than 10", e);
       }
       catch(Exception e)
       {
           message.setHeader("FAILE", "TRUE");
           
       }


       return message;
}

digirolamocristian60
Participant
0 Kudos

Dear Vijay Kumar Kapuganti,

thank you for the answer. What if the exception occurred not in the main script but in a function called within the main script? I put the try-catch block even in that function but I saw same behavior. My exception is a java.lang.NullPointerException and I'm not able to catch it. Maybe you can give me a hint.

Thanks.

Cristian

former_member666312
Active Participant
0 Kudos

Hi ,

That is also working for me it seems. Please check the sample script.



import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
    def messageLog = messageLogFactory.getMessageLog(message);
    
    String testInput = "KRISHNA";
    String output = test(testInput, message);
    messageLog.setStringProperty("FROM_CALLIG_METHOD", output)
    //Body 
    /*
        
       def body = message.getBody();
       def map = message.getHeaders();
       def head = map.get("head");
       
         messageLog.setLongProperty("Logging#1", head.length())
       
       try
       {
           if(head.length() > 10)
           {
               message.setHeader("YOU_ARE_RIGHT" , head);
               
               
           }
           else
           throw new RuntimeException("head length is less than 10", e);
       }
       catch(Exception e)
       {
           message.setHeader("FAILE", "TRUE");
           
       }*/


       return message;
}


public String test(String  input, Message message)
{
       def head = input;
       def messageLog = messageLogFactory.getMessageLog(message);
         messageLog.setLongProperty("Logging#1", head.length())
       
       try
       {
           if(head.length() > 10)
           {
               message.setHeader("YOU_ARE_RIGHT" , head);
               
               
           }
           else
           throw new RuntimeException("head length is less than 10", e);
       }
       catch(Exception e)
       {
           message.setHeader("FAILE", "TRUE");
                      
       }
       return head;
}



digirolamocristian60
Participant
0 Kudos

Dear Vijay Kumar Kapuganti,

I used both the try-catch block you showed but still it doesn't work. Maybe because the script is running within a subprocess with a standard exception process in it? Could the exception subprocess prevent the try-catch block in the script from working? If not, maybe it's just a problem with the tenant I'm working on.

Regards.

Cristian