Sample code for exception handling
The following code is a sample for exception handling to guide you on appropriate handling of exceptions.
Sample scripting based import
Sample code for exception handling in a scripting based import script:var parser = newCSVParser(in);
var bDone = false;
while(!bDone)
{
var attrs = parser.splitLine();
bDone = (null == attrs);
if (!bDone)
{
catchError(e, errObj)
{
// The user-written function getNewItemUsingAttrs()
// populates an item object from the attrs
var item = getNewItemUsingAttrs(attrs);
item.saveCtgItem();
}
if (errObj != null)
{
// The user-written function isCriticalError() uses Java to interrogate errObj
if (isCriticalError(e, errObj))
{
throwError("Critical error occurred in import job: " + e);
}
}
}
}
Sample scripting based report
Sample code for exception handling in a scripting based report script. This applies to a report script written to import items:var bDone = false;
var numItemsInTxn = 0;
var numItemsPerTxn = 200;
while(!bDone)
{
catchError(e, errObj)
{
useTransaction
{
numItemsInTxn=0;
while (!bDone && numItemsInTxn < numItemsPerTxn)
{
// The user-written function getNextItemToSave()
// returns the next item object to persist
var item = getNextItemToSave();
bDone = (null == item);
if (!bDone)
{
item.saveCtgItem();
numItemsInTxn++;
}
}
}
}
if (errObj != null)
{
// The user-written function isCriticalError() uses Java to interrogate errObj
if (isCriticalError(e, errObj))
{
throwError("Critical error occurred in import job: " + e);
}
}
}