RAS Playground 日本語 GitHub

RAS standard library

Errors and event handlers

These are types built into the compiler.

export function safeDivide(a: int, b: int): int {
    try {
        if (b == 0) throw new Error("Cannot divide by 0");
        return a / b;
    } catch (e) {
        debug.print(e.code, e.description, e.rasFile, e.rasLine);
        return 0;
    }
}

export function watchInput() {
    const listener = Sheet1.on("Change", (target) => {
        debug.print(target.address);
    });
    listener.off();                       // detach
}

VBA's On Error Resume Next is written as try { … } catch { resume; }. For finally and using, see specification §5.

Reference

Interface RAS_Error

Error information received by catch (e).

Create one with throw new Error("...") and rethrow it with throw e;.

#
code: int

Error number (VBA's Err.Number).

#
description: string

Description of the error (VBA's Err.Description).

#
readonly message: string

Same as description.

#
source: string

Source of the error (VBA's Err.Source).

#
rasFile: string

The RAS source file where the error occurred.

#
rasLine: int

Line number in the RAS source where the error occurred (0 when line numbering is off).

core.d.ras

Interface RAS_Listener

An event handler attached with on / addEventListener.

Remove it with off(). Handlers attached in a form's code-behind are removed automatically when the form closes.

#
off(): void

Removes the event handler. It is not called afterwards (calling this more than once is fine).

core.d.ras