Guidelines for exception handling in scripting
You can use the catchError()
script operation to catch exceptions in the
scripting code, and take the required action that is based on the exception. You can choose to
handle the exception programmatically or rethrow the exception to calling code.
It is recommended that you handle the exceptions in the following manner:
- If the exception message string indicates a failure from a business error that can be programmatically handled by the script implementation, then you can choose to handle it.
- If the exception is received from starting an IBM® Product Master operation, and you determine the root cause
of the exception to be an SQLException, then you must ensure that the active transaction is
rolled-back.
- If the scripting code results in the exception is contained within a
useTransaction()
block, then the exception must be thrown beyond the closing brace ofuseTransaction()
to ensure rollback of current transaction.catchError(e, eObj) { useTransaction() { //your code } } if (e != null) { //it is safe to handle the exception, since the transaction has been rolled-back }
- If the scripting code results in the exception is contained within a
startTransaction()
block, then the exception must be rethrown to the calling code if an active transaction existed whenstartTransaction()
was started.var existingTxn = inTransaction(); catchError(e, eObj) { startTransaction() { //your code } } if (e!= null) { if ((existingTxn == true) && isSQLException(eObj)) { throwSQLException() } // otherwise it is safe to handle the exception since the transaction owned by us has been rolled-back }
- If the scripting code results in the exception is not contained within a transaction block, then
the exception must be rethrown from calling code if an active transaction existed when the code was
started.
var existingTxn = inTransaction(); catchError(e, eObj) { //your code } if (e != null) { if ((existingTxn == true) && isSQLException(eObj)) { throwSQLException() } }
- Since throwError script operation does not retain the root cause exception, you can prefix a
string to indicate to the calling code that it is an SQL
Error.
function throwSQLException(e) { throwError("SQL ERROR OCCURRED: " + e); }
The method isSQLException(eObj) can be implemented by creating a piece of Java™ code that performsinstanceOf
on the exception object to detect if it is a SQLException. Alternatively, the following code can be used to return the error message from the root cause exception object.
The error message can be inspected for an identifying substring, such as "SQL Error", to determine whether the causative exception is an SQLException.var method = createJavaMethod("java.lang.Throwable", "getMessage"); errorMessage = runJavaMethod(eObj, method);
- If the scripting code results in the exception is contained within a