Exception Handling

When running JavaScript, various runtime errors can occur. By default, JavaScript halts execution and reports an error when a problem is encountered. Errors may arise from programmer mistakes, invalid input, or unpredictable issues such as exceptions thrown by ControlCommand() when controlling other robot skills. Using try...catch (and optionally finally) allows you to handle these exceptions gracefully so your application or robot skill can respond or recover.

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

The try, catch, finally, and throw statements provide structured error handling in JavaScript:

  • try — Defines a block of code to execute and test for errors. If an exception occurs inside this block, control passes to the associated catch block.
  • catch — Defines a block that handles any exception thrown in the try block. The catch block receives the thrown value (commonly an Error object) so you can inspect or log details.
  • finally — Defines a block that always runs after try and catch, regardless of whether an error occurred. Use finally for cleanup tasks (closing resources, stopping motors, releasing locks, etc.).
  • throw — Allows you to create and raise a custom exception (a string, Error object, or any value). Use throw to signal conditions that should be handled by higher-level code.

Example flow: try to execute a ControlCommand(); if it fails, catch can log the error or attempt a fallback; finally can perform cleanup actions such as resetting variables or stopping hardware safely.

The Error Object

JavaScript provides a built-in Error object that delivers information about runtime problems. When an exception is thrown as an Error (or one of its subclasses), the error value typically exposes two useful properties:

  • name — The type or name of the error (for example, "TypeError").
  • message — A descriptive message explaining the error (a string).

In addition, Error objects may include other properties such as stack in many environments, which helps with debugging by showing the call stack.

Error Object Properties

Property Description
name Sets or returns the error name (for example, "ReferenceError").
message Sets or returns the error message (a string describing the error).

Error Name Values

The name property commonly indicates one of several built-in error types. These are the standard names you may encounter:

Error Name Description
EvalError An error related to the eval() function (rare in modern code).
RangeError A numeric value is outside an allowable range (for example, invalid array length).
ReferenceError An invalid reference was used (such as accessing an undefined variable).
SyntaxError Code contains a syntax error and cannot be parsed.
TypeError An operation was performed on a value of an inappropriate type.
URIError An error occurred during URI encoding/decoding (for example, malformed percent-encoding).

Example #1

This example forces an error using throw and demonstrates a simple try/catch. In a robot skill, you might replace print(err) with a log, an alert, or a fallback command.

try {
  throw "this is an error";
} catch (err) {
  print(err);
} finally {
  // Optional cleanup code here
}