- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2020 09:07 AM
Hi,
I can't catch this stupid error :
Let's say I initialize this GlideRecord in a stupid way. When calling setValue method throws an error that I am not able to catch.
Why ? Thanks
// ----------------------------------------------------------------------
var gr=new GlideRecord(null); // Missing table name
try
{
gr.setValue(100,"toto");
}
catch(e)
{
gs.log("Never goes here.");
}
// ----------------------------------------------------------------------
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2020 09:18 AM
Hi,
That's because no error is thrown? I believe.
You can try:
// ----------------------------------------------------------------------
var gr=new GlideRecord(null); // Missing table name
gr.query();
try
{
gr.setValue(100,"toto");
}
catch(e)
{
gs.log("Never goes here.");
}
// ----------------------------------------------------------------------
So I added in a gr.query(); to now get it going.
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-22-2020 09:17 AM
Hi Allen,
You are absolutely right. In fact, I did not see, there is indeed an error message which plants the script but it is not strictly speaking a throw I think. Which explains why we don't go into the catch. the isValid() does the trick well.
Thanks.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2020 09:30 AM
Hi Bruno,
For me it works when you add gr.query() as told by Allen inside the try block.
var gr=new GlideRecord(null); // Missing table name
try
{
gr.query(); //Adding this inside the try block
gr.setValue(100,"toto");
}
catch(e)
{
gs.log("Never goes here.");
}
Please mark it correct and helpful if it works.
Thanks,
-Akash
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2020 10:12 AM
Hi Bruno,
You may find the below thread helpful.
https://community.servicenow.com/community?id=community_blog&sys_id=a33e62addbd0dbc01dcaf3231f961952
Hope this helps!
Please mark the reply as Helpful/Correct, if applicable.
Regards,
Hemant

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2020 11:18 AM
Hi Bruno
as per
try-catch-finally is used to handle runtime errors and prevent them from halting the execution of a program.
1. Return statement inside the try or catch block:-
If we have a finally
block, the return
statement inside try
and catch
block are not executed. It will always hit the finally
block.
2. Variables declared inside a try block are not available in the catch or finally block
If we use let
or const
to declare a variable in the try
block, it will not be available to catch
or finally
. This is because these variable declarations are block-scoped.
3. Catch without error details
In ES2019, the argument for the catch
block is optional.
4. try…catch will not work on setTimeOut
If an exception happens in “scheduled” code, like setTimeout
, then try..catch
won’t catch it.
5. Adding a global error handler
We can register a window.onerror
event listener that will run in case of an uncaught error. This will not handle the error but detect it— you would need to supply your own try…catch
inside of it.
In the above link,you will get simples examples to learn:-
Please Check the Video from Chuck Tomasi for your issue.
https://www.youtube.com/watch?v=-PAI9jmUWMM&list=PL3rNcyAiDYK2_87aRvXEmAyD8M9DARVGK&index=27
Please Mark Correct and Helpful
Thanks and Regards
Gaurav Shirsat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-21-2020 11:31 AM
Yeah, it's kind of an odd thing that an error is not thrown, but you can check to see if the table exists or not with the "isValid()" method:
(function() {
var gr=new GlideRecord(null); // Missing table name
if (gr.isValid()) {
try {
gr.setValue(100,"toto");
} catch(e) {
gs.log("Never goes here.");
}
} else {
gs.error("not a valid table");
}
})();