Inherited from v1.0.0

throw keyword

throw raises an error and unwinds the stack until a matching try / catch is found. The Bangla form নিক্ষেপ is equivalent.

Bnlang has no built-in Error class — you can throw any value. By convention, throw a descriptive string.

Syntax

throw <expression>;

Examples

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

try {
    print(divide(10, 0));
} catch (e) {
    print("caught:", e);   // caught: divide by zero
}

Throwing a map

For richer errors, throw a map with a code and message:

function fetch_user(id) {
    if (id == null) {
        throw { code: "BAD_INPUT", message: "id is required" };
    }
    // ...
}

try {
    fetch_user(null);
} catch (e) {
    print(e.code, "-", e.message);   // BAD_INPUT - id is required
}

Uncaught throws

A throw that is never caught terminates the program with a non-zero exit code, printing the thrown value and the source location.

Bangla form

ফাংশন divide(a, b) {
    যদি (b == 0) {
        নিক্ষেপ "divide by zero";
    }
    ফেরত a / b;
}

See also