Using try catch in workflow doesn't catch errors

angus_palmer
Kilo Contributor

I'm seeing some strange behaviour with a try catch block in a workflow script activity that is calling functions in an object from a script include. In short, a thrown error is only caught during the object initialize. If the error is thrown during another function call after 'new' then it isn't caught and causes the workflow activity to end in error.

I was able to replicate the problem with simplified code. This workflow is attached to a Requested Item and I'm working with Fuji, glide-fuji-12-23-2014__patch13-hotfix1-05-26-2016

Catch doesn't work

Script Include

var ErrorThrower = Class.create();

ErrorThrower.prototype = {

      initialize: function () {},

     

      throwAnError: function () {

              throw new Error('throwAnError() I throw errors.');

      },

      type: 'ErrorThrower'

};

Workflow Script

try {

      var et = new ErrorThrower(current);

      et.throwAnError();

} catch (e) {

      workflow.error('## Caught An Error ## ' + e); // Does not get here

}

This caused the workflow activity to change to an Error state and the following error in the workflow log;

Caught exception in InterpretedScript <refname>: org.mozilla.javascript.JavaScriptException: org.mozilla.javascript.InterpretedScriptException: Caught exception in InterpretedScript #0(eval): org.mozilla.javascript.JavaScriptException: Error: throwAnError() I throw errors.

Catch works

Script Include

var ErrorThrower = Class.create();

ErrorThrower.prototype = {

      initialize: function () {

              throw new Error('initialize() I throw errors.');

      },

      type: 'ErrorThrower'

};

Workflow Script

try {

      var et = new ErrorThrower(current);

} catch (e) {

      workflow.error('## Caught An Error ## ' + e); // This works as expected

}

This allows the workflow activity to complete and enter a Finished state. As expected the following error is found in the workflow log.

## Caught An Error ## Error: initialize() I throw errors.

This question seemed related to mine, How to use try catch in ServiceNow Scripts   but I haven't been able to find any further information about Steve Ball's K16 session.

I also tried a nested try...catch wrapping the throwAnError function in its own try block and then throwing the error from the nested catch. This gave me the same InterpretedScriptException as shown above.

However running this as a background script works. e.g

Background script

try{

var v = new ErrorThrower();

v.throwAnError();

}catch(e){

gs.print(e); // This returns: *** Script: Error: throwAnError() I throw errors.

}

The question is, what is going on here? Am I missing something fundamental with Exceptions in ServiceNow?

2 REPLIES 2

Fred Jean
Tera Expert

Hello,


As I came across the same issue I reported it to servicenow support.


Here's their answer :



"This is a known problem - PRB657870 which affects Fuji and Geneva release.


The root cause was a Fuji change made in Rhino that was backed out as part the Helsinki "new Rhino" initiative.


There is no workaround.



This problem is fixed in Helsinki."



Regards,


Thanks Frederic, I found it fixed in Helsinki too and fortunately we have since upgraded our prod instances