Documentation Menu

Exception Object

Allows you to create custom exceptions that hold a JSON object and an inner exception.

Definition

new JL.Exception(data: any, public inner?: any)

Parameters

data String or object to be stored in the exception, or a function that returns the string or object to be stored in the exception.
inner Exception to be stored inside this exception.

Remarks

The JSNLog Exception object is a more powerful version of JavaScript's Error object. For comparison sake, the Error object looks like this:

new Error(message?: string)

The Exception object differs from the Error object in these ways:

  • Unlike the Error object's message parameter, the Exception object's data parameter can take not only strings, but also JSON objects and functions. If you pass in a function, it will be executed. The function's return value will be used as the value of the exception.
  • You can store an inner exception, as shown in this example.

Examples

Simple try-catch, using console log to write the exception message to the console:

try {
    // Exception value is an object
    throw new JL.Exception({ "i": 1, "j": 2 });
} catch (e) {
    // Logs the message:
    // Uncaught JL.Exception: {"i":1,"j":2}
    console.log(e.message);
}