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);
}RunYou 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.