cancel
Showing results for 
Search instead for 
Did you mean: 

UDF to check and raise exception

Former Member
0 Kudos

Hi Experts,

The occurrence of target structure is 0..unbounded, and I am trying an UDF to check whether the input is multiple of 10, if it is a multiple of 10 then actual input value should be returned else if it is not a multiple of 10 then an exception should be raised.I tried with below code but it throws errors while activating it.

public String CheckValue(String var1, Container container) throws StreamTransformationException{

if (!(var1%%10).equals("0")) {

    return var1;

} else {

    throw new StreamTransformationException("Here is error message");

}

}

The errors I get are as below:

"Function CheckValue, Line 1:

operator % cannot be applied to java.lang.String,int if (!(var1%10).equals("0")) {           ^ Note: /usr/sap/XYZ/J00/j2ee/cluster/server0/./temp/classpath_resolver/Mapfbe07f76eb4511e5ccf70000006640c2/source/com/sap/xi/tf/_yyyyyyyyyyy.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error "

Thanks,

Nithin.

Accepted Solutions (1)

Accepted Solutions (1)

engswee
Active Contributor
0 Kudos

Hi Nithin

You need to declare your input as int instead of String. And your comparison needs to be != instead of equals(). Then you need to convert back the integer to string for your return.

public String CheckValue(int var1, Container container) throws StreamTransformationException{

if (var1%10 != 0) {

    return Integer.toString(var1);

} else {

    throw new StreamTransformationException("Here is error message");

}

}

Former Member
0 Kudos

Hi Eng,

Haven't worked with PI for a while, but if I remember correctly, parameters in the UDF are fixed as type String.... and we have to convert the string to int in the body of UDF like below:

public String CheckValue(String var1, Container container) throws StreamTransformationException{


int var = Integer.parseInt(var1);


if (var % 10 != 0) {

    return Integer.toString(var);

} else {

    throw new StreamTransformationException("Here is error message");

}

}

engswee
Active Contributor
0 Kudos

Nope. There are a few choices of parameter types to choose from.

Former Member
0 Kudos

Answers (1)

Answers (1)

iaki_vila
Active Contributor
0 Kudos

Hi Nithin,

Why var1%10?

You have in Debashish Roy answer a code perfect for you UDF to raise exception | SCN

Regards