
- 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-07-2015 08:03 PM
P.S.
e.stack returns undefined
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 12:54 AM
Hi Paul,
GlideLog has a method for dumping the stack trace in the log files:
GlideLog.dumpStack();
Regards,
Sergiu

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2015 01:40 AM
Ironically, an exception is thrown when I try and use that method.
Attempted script access to inaccessible member denied - com.glide.util.Log:dumpStack:()V
Evaluator: java.lang.SecurityException: Illegal access to method dumpStack() in class com.glide.util.Log
Caused by error in script at line 9
6: gss.fakeMethod();
7:
8: } catch (e) {
==> 9: GlideLog.dumpStack();
10: gs.print('caught' + e );
11:
12:
Evaluator: java.lang.SecurityException: Illegal access to method dumpStack() in class com.glide.util.Log
Caused by error in script at line -1
Background message, type:error, message: Illegal access to method dumpStack() in class com.glide.util.Log
Anyway, I'm looking for a way to capture the output of the stack trace of the last caught exception so I can put it in my own logs.
I need to store it as a string temporarily.
The log files are not reliable enough.
Does anyone have any ideas?
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 02:07 AM
Paul,
What about this type of exercise:
try {
foo.somethingThatThrows();
} catch (err) {
gs.error("There was an error doing something", err);
}
gives me on my own instance:
[0:00:00.004] Script completed in scope global: script
*** Script: There was an error doing something: ReferenceError: "foo" is not defined.: no thrown error
Is this good enough?
Regards,
Sergiu