Types of exceptions that can be handled by tri-catch

bonsai
Mega Sage

* This is an instance of the Paris version.

 

I'm thinking of writing a script to insert a record.
I want to implement exception handling with "try ~ catch",
In the following cases, no error has occurred.

(1) When a character string other than the date format is set in the field for entering the date
⇒ A blank is set in the field and no error occurs.

(2) When 100 characters are entered in the field for entering a character string with a 40-character limit.
⇒ The first 40 characters of 100 characters are set, and no error occurs.


I thought that "Type Error" would occur in case of (1) and "Range Error" would occur in case of (2).

Does anyone understand?

1 ACCEPTED SOLUTION

Maik Skoddow
Tera Patron
Tera Patron

Hi 

it seems, that you are not familar with try-catch-blocks. Please read https://www.w3schools.com/js/js_errors.asp

Basically you catch pure JavaScript errors and ServiceNow APIs only throw real errors in rare cases. Unfortunately, you have to check the failure behavior for each method of an ServiceNow-API. Sometimes they return null, sometimes empty Strings and sometimes an error is thrown.

Kind regards
Maik

View solution in original post

3 REPLIES 3

Maik Skoddow
Tera Patron
Tera Patron

Hi 

it seems, that you are not familar with try-catch-blocks. Please read https://www.w3schools.com/js/js_errors.asp

Basically you catch pure JavaScript errors and ServiceNow APIs only throw real errors in rare cases. Unfortunately, you have to check the failure behavior for each method of an ServiceNow-API. Sometimes they return null, sometimes empty Strings and sometimes an error is thrown.

Kind regards
Maik

thank you for your answer.
I learned a lot! !!

Is there any other way to control ServiceNow's API-related errors like "Try ... Catch"?

Hi

although I had not the time yes to write an article about error handling, you can see the underlying concepts in my article Universal Pattern for Script Includes

At the end it is always good to have a wrapping try-catch-block for the whole script. And within the try-block you have to handle all other error behaviors manually.

Kind regards
Maik