Skip to content
10 of 15

Errors ​

throw stops the current function with an error, and try/catch handles it. Errors carry a message:

pleno
fn withdraw(balance: Float, amount: Float): Float {
    if (amount > balance) {
        throw Error("not enough money");
    }
    return balance - amount;
}

try {
    withdraw(10.0, 50.0);
} catch (e) {
    print("Refused:", e.message);
}
Run

You can define your own error classes with class NotFound extends Error {}, and catch them by type: catch (e: NotFound). A finally block always runs.

Your turn ​

parsePort throws for invalid ports. Catch the error and print Invalid: ... instead of letting the program stop.

errors.pln
fn parsePort(text: String): Int {    let port = text.toInt();    if (port < 1 || port > 65535) {        throw Error(`${text} is not a valid port`);    }    return port;} print(parsePort("8080"));print(parsePort("70000"));