How do I catch exceptions within an ECMA script?[Keywords : tryEval]

How do I catch exceptions within an ECMA script?[Keywords : tryEval]

book

Article ID: KB0091474

calendar_today

Updated On:

Products Versions
TIBCO IntegrationManager -
Not Applicable -

Description

Resolution:
You can use the FESI extension tryEval() to do that. Following is the syntax and usage description for tryEval():

tryEval(string[,default])

tryEval evaluates the parameter as eval (similar to exec) but returns an object with two properties: value and error. If the evaluation is successful, the value property contains the result of the evaluation and the error property is null (which tests as false). If the evaluation results in an error, then the value property is either the value of the default parameter (if specified) or undefined, and the error property contains an object describing the error, which is guaranteed to test as true. The error object's string representation is some description of the error.

Here is an example of how to use tryEval()...

Implement the following in the ECMA Script task:

{
st = new java.lang.String("Hello world");
writeln("Length : " + st.length());
var trial = tryEval("st.substring(3)")
writeln("here");
if(trial.error)
writeln(trial.error);
else
writeln("no error");
}


When executed, this will print 'no error'.

Now change the following statement:
var trial = tryEval("st.substring(3)")
to
var trial = tryEval("st.substring(30)")

i.e we are trying to get a substring starting from index 30, which does not exist.

When executed it will print out the exception.

Issue/Introduction

How do I catch exceptions within an ECMA script?[Keywords : tryEval]

Environment

Product: TIBCO IntegrationManager Version: 4.5.0 OS: All --------------------