Why is my try catch not catching the RuntimeException?

peterraeves
Mega Guru

Something very very weird is going on. Despite my try/catch block, the RuntimeException is still thrown and not caught... How do I catch it?

Evaluator: java.lang.RuntimeException: String object has exceeded max size of 16777216

  Caused by error in script at line 18

  15: var b64 = GlideBase64.encode(bytearrayOS.toByteArray());

  16: try {

  17: var newb64 = b64.split(String.fromCharCode(10)).join('');

==> 18: newb64 = newb64.split(String.fromCharCode(13)).join('');

  19: gs.print(att.getDisplayValue() +':\n'+newb64);

  20: } catch (err) {

  21: gs.print('ignoring exception')

  22: }

The second weird thing is, that it only triggers on line 18, but not on line 17. Why is the string not too long in line 17, but too long in line 18? It should be shorter, no?

1 ACCEPTED SOLUTION

sergeypovisenko
Giga Contributor

ServiceNow backend is based on Java.
And when you are using Java's classes, such String, executing of this part is delegating on Java backend and in your case it throws RuntimeException (Java Platform SE 7 ) because String object contains an array of chars, so you can have at most 2^31 - 1 characters in a Java String. In this case, error occurs in scope which is not visible for your JavaScript try-catch.
If you throw an exception explicitly in JavaScript code block, your catch will works. You can try


View solution in original post

2 REPLIES 2

sergeypovisenko
Giga Contributor

ServiceNow backend is based on Java.
And when you are using Java's classes, such String, executing of this part is delegating on Java backend and in your case it throws RuntimeException (Java Platform SE 7 ) because String object contains an array of chars, so you can have at most 2^31 - 1 characters in a Java String. In this case, error occurs in scope which is not visible for your JavaScript try-catch.
If you throw an exception explicitly in JavaScript code block, your catch will works. You can try


That really sucks...