
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2015 08:00 PM
Does anyone know if it is possible to print a stack trace out from a try/catch? I want to find out the line of code to store in an error record for my application.
Example code:
try {
var gr = new GlideRecord('incident');
var test = gr.madeUpFunction();
test.adssad();
} catch (e) {
var stackTrace = e.????;
gs.print('Stack trace: ' + stackTrace);
}
Expected output:
Evaluator: org.mozilla.javascript.EcmaError: "est" is not defined.
Caused by error in script at line 4
1:
2: var gr = new GlideRecord('incident');
3: var test = gr.madeUpFunction();
==> 4: est.adssad();
Evaluator: org.mozilla.javascript.EcmaError: "est" is not defined.
Caused by error in script at line -1
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2015 11:10 PM
Hi Paul, the following is what I believe you have available for an exception:
try {
var gr = new GlideRecord('incident');
madeUpVariable.madeUpFunction();
} catch (e) {
gs.print('LineNumber :' + e.lineNumber);
gs.print('SourceName :' + e.sourceName);
gs.print('Name: ' + e.name);
gs.print('Message : ' + e.message);
}
outcome:
*** Script: LineNumber :3
*** Script: SourceName :<refname>
*** Script: Name: ReferenceError
*** Script: Message : "madeUpVariable" is not defined.
Thanks,
Berny

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2015 11:13 PM
Would be great if this was documented -_-
Thanks!
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2015 11:16 PM
Indeed !
Well, thanks to your great question I guess we were able to sort of "document" this for future reference.
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2015 11:11 PM
I hope you're doing great! and i hope that's helpful.
Thanks,
Berny

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-09-2015 02:19 AM
Even though this is already answered, but for more info on this, an example of stack trace can be found in 'SOAPRequest' script include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2025 12:48 PM - edited 01-28-2025 12:49 PM
I know this is old but it's still a problem.
Also, for those that don't care to look it up 'SOAPRequest' utilizes e.getStackTrace() which seems to return an array of Java objects for each stack level:
try {
//...
} catch(e) {
var sb = new Packages.java.lang.StringBuffer();
var st = e.getStackTrace();
for (var i = 0; i < st.length; i++) {
sb.append(st[i].toString() + "\n");
}
gs.error("stack_trace :" + sb.toString());
}
This doesn't work in the cases where you throw an error yourself or rethrow a caught error to provide more context. i.e. throw new Error("...") or throw "..." probably because the OOB (out of the box) errors are all Java based objects and you have no access to most of those.
What you could potentially try is to capture the native OOB errors at the lowest level possible in code, get the stack trace using the method above, and include it in the error message when rethrowing. If you need to trace the message up though multiple re-throws in user code you'll need to pre-pend or append info or lines to the error message "stack".
If you're throwing an error yourself, capture the current stack using the following call:
GlideLog.getStackTrace(new Packages.java.lang.Throwable())
and include that in the thrown error message as previously described.
I've actually made this into an "ErrorTracer" script include which just modifies the incoming error message all the way up. It's hacky but it's better than nothing.
It would be nice if there was an out of the box way to throw an actual Java exception that captured the stack trace and could be rethrown as an "inner exception" similar to pretty much every other programming language.