cancel
Showing results for 
Search instead for 
Did you mean: 

How to handle Exceptions in user Defined Functions

former_member187437
Contributor
0 Kudos

Hi All,

I would like to know how to handle excpetions raised in user defined functions.

Suppose, i have UDF to remove a character at the given index of the given input String. This UDF has two inputs, the String and the index.

During the String Processing, there might b many exceptions possible like IndexOutOfBound.

In such a case how to handle this?

If I catch the exception, then some value must be returned by the UDF and the mapping will be executed even if the return value is incorrect and the scneario willb executed. So is this the right way ? Or should the exception be left with no handler so that runtime error wil occur preventing hte execution of whole scenario.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hey

Use the try-catch block in your UDF

try

{

/your logic here/

}

catch (Exception error)

{

throw new RuntimeException(error);

}

Thanx

Aamir

former_member187437
Contributor
0 Kudos

Hi All,

Thanks for all your quick responses.

but one more doubt. If I m catching it and again throwin a new Exception , this will prevent the exectuion of the sceanrio.But whats the use of catch Block?

Even if there is no handler, the exception message will be shown and the scenario will be stopped.

henrique_pinto
Active Contributor
0 Kudos

Well, usually when the map fails it should halt the process!

If your desire is e.g. just to give a warning, use something like:


AbstractTrace trace = container.getTrace();
try {
//...
} catch (Exception e) {
  trace.addWarning("...");
}

instead of throwing a new excetpion.

Regards,

Henrique.

Former Member
0 Kudos

Aarthi,

It purely depends on your scenario & Business requirement.

Whether you want to throw the exception (or) not.If suppose if it some mandatory field which is required then you can throw exception.

If the Data is Optional then you can catch and return some default value. Instead of throwing exception

Regards

Ganga

Answers (2)

Answers (2)

Former Member
0 Kudos

It is better to handle exceptions.

Use a try-catch block

try

{

your code

}

catch(Exception e)

{

result.addValue( "EXCEPTION CAUGHT : " + e.getMessage());

gc.setParameter("STATUS" , "1");

return;

}

Here gc is the global container which helps to set global variables.When the return value is incorrect these global variables can be set and mapping can be executed accordingly.

When we use exceptions we come to know the error in our inputs.

Former Member
0 Kudos