Exception Handling

When executing JavaScript code, different errors can occur. JavaScript will normally stop and generate an error message when an error occurs. These can be coding errors made by the programmer, errors due to wrong input, and other unforeseeable things such as ControlCommand() exceptions. If executing a ControlCommand() to instruct another robot skill and it fails, a Try Catch can handle the exception error.


Throw, and Try...Catch...Finally

  • The try statement defines a code block to run (to try). This allows you to define a block of code to be tested for errors while executing it.
  • The catch statement defines a code block to handle any error. This defines a block of code to be executed if an error occurs in the try block.
  • The finally statement defines a code block to run regardless of the result. This statement lets you execute code after try and catch, regardless of the result:
  • The throw statement defines a custom error.


The Error Object

JavaScript has a built-in error object that provides error information when an error occurs. The error object provides two useful properties: name and message.


Error Object Properties

PropertyDescription
nameSets or returns an error name
messageSets or returns an error message (a string)


Error Name Values

The error name property can return six different values:

Error NameDescription
EvalErrorAn error has occurred in the eval() function
RangeErrorA number "out of range" has occurred
ReferenceErrorAn illegal reference has occurred
SyntaxErrorA syntax error has occurred
TypeErrorA type error has occurred
URIErrorAn error in encodeURI() has occurred


Example #1

This example forces an error using the throw statement.

try {

  throw "this is an error";
} catch(err) {

  print(err);
}