Inherited from v1.0.0

try / catch / finally

A try block contains code that may throw. A trailing catch (<name>) { ... } receives the thrown value. An optional finally { ... } block runs regardless of whether the try succeeded or threw — useful for cleanup. The Bangla forms চেষ্টা / ধরুন / অবশেষে are equivalent.

Syntax

try {
    // code that may throw
} catch (<name>) {
    // runs only when the try block threw; <name> binds the thrown value
} finally {
    // runs in both cases (optional)
}

catch is required; finally is optional.

Examples

function divide(a, b) {
    if (b == 0) { throw "divide by zero"; }
    return a / b;
}

try {
    var result = divide(10, 0);
    print(result);
} catch (e) {
    print("caught:", e);
} finally {
    print("done");
}

// Output:
// caught: divide by zero
// done

Cleanup with finally

finally is the right place for resource cleanup so it always runs:

import "io" as io;

var f = io.open_read("notes.txt");

try {
    f.read(64, function (err, chunk) { /* ... */ });
} catch (e) {
    print("read failed:", e);
} finally {
    f.close();
}

Catching specific shapes

The catch parameter is just a plain value — inspect it to branch:

try {
    do_work();
} catch (e) {
    if (type(e) == "map" and e.code != null) {
        print("error code:", e.code, "-", e.message);
    } else {
        print("unexpected error:", e);
    }
}

Rethrowing

You can re-throw the caught value (or a new one) from inside catch:

try {
    risky();
} catch (e) {
    print("logging error:", e);
    throw e;   // bubble up
}

Bangla form

চেষ্টা {
    var result = divide(10, 0);
} ধরুন (e) {
    print("caught:", e);
} অবশেষে {
    print("done");
}

See also

  • throw — raise an error.