Error Handling in Scripts

JavaScript comes with built-in exception handling capabilities. These exception handlers make it possible to catch errors and resolve them internally, which insulates the user from complex decisions and cryptic technical information.

You can throw exceptions using the throw statement and handle them using the try...catch statements (JavaScript 1.5 or higher).

Statement Description
throw

Use the throw statement to throw an exception. When you throw an exception, you specify an expression containing the value of the exception:

throw expression
try...catch The try...catch statement marks a block of statements to try, and specifies one or more responses should an exception be thrown. If an exception is thrown, the try...catch statement catches it.

Conditional Catch Statements

The B2C Commerce JavaScript Engine supports Mozilla's proprietary extension of conditional catch statements. Conditional catch statements can be compared with typed catch statements in Java.

try {
   ...
}
catch( e if e instanceof IOError ) {
   ...
}

In this example, the exception is only caught if the thrown object is an IOError. All other exception objects that don't meet the condition bubble up to other exception handlers. The condition can be any boolean expression, even an expression as in the following code example:

try {
   ...
}
catch( e if e instanceof IOError && e.javaName == 'TimeoutException' ) {
   ..
}

In this example, B2C Commerce's extension to the error classes, which adds information about the underlying Java exception, is used to further constrain the catch statement.

Server-Side JavaScript Error Handing

Specific classes in the top-level API script package handle server-side JavaScript errors, as follows:

Exception Type Description
Conversion Error Represents a conversion error. For example, between string and JavaScript objects.
Error Error represents a generic exception.
EvalError Represents an evaluation error. The eval() function is used in an incorrect manner.
Fault Indicates an RPC-related error in the system. It's always related to a systems internal Java exception. In particular, it provides details about the error sent from the remote system.
InternalError Represents an internal error.
IOError Indicates an I/O related error in the system. It's always related to a systems internal Java exception. The class provides access to more details about this internal Java exception.
RangeError Represents a range error. A numeric variable exceeds its allowed range.
ReferenceError Represents a reference error. An invalid reference is used.
SyntaxError Represents a syntax error. A syntax error occurs while parsing JavaScript code.
SystemError This error is always related to a systems internal Java exception. This error indicates an error in the system, which doesn't fall into any of the other categories, such as, IOError. The class provides access to more details about this internal Java exception.
TypeError The type of a variable isn't as expected.
URIError The encodeURI() or decodeURI() functions are used in an incorrect manner.
XMLStreamError This error is always related to a systems internal Java exception. This error indicates an XML streaming-related error in the system. The class provides access to more details about this internal Java exception. In particular, the class describes the location of the error.
Note: The italicized errors are the six primary ECMAScript exception types defined by the JavaScript 1.5 specification.

Internal JavaExceptions

Beyond the standard JavaScript error classes, B2C Commerce maps internal JavaExceptions to the following errors in JavaScript:

Error Description
IOError IOException, SOAPException, and all subclasses
Fault SOAP fault
XMLStreamError Errors related to an XML stream
SystemError All other Java Exceptions
Note: In general, Java errors such as an OutOfMemoryException, can't be caught in scripting code. See Java concerning exception versus error.