
- 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 06:11 PM
It is not enough information.
I'm deploying an application at my company that is still in early stages and I really need to know exactly where in my code the errors are being thrown.
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
03-27-2023 02:39 AM
Hi @The SN Nerd
There is another callable function in GlideLog:
gs.log(new GlideLog.getStackTrace(new Packages.java.lang.Throwable()));
See also https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0683765
If this answer was helpful, I would appreciate if you marked it as such - thanks!
Best
Daniel

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2015 01:55 AM
This does not throw me any error. Do I need to enable any property for this to throw error?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2015 06:11 PM
Example code is meant to be this sorry
- try {
- var gr = new GlideRecord('incident');
- madeUpVariable.madeUpFunction();
- } catch (e) {
- var stackTrace = e.????;
- gs.print('Stack trace: ' + stackTrace);
- }
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: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