Quick start
Example: Safe Calculator
This example introduces LaadleLang's robust error-handling infrastructure. It uses the gopgop keyword to explicitly throw errors, and the koshish / pakad (try/catch) block to intercept and gracefully recover from them rather than crashing the virtual machine.
The Code
// A dangerous math operation that will throw an error if abused
kaam safe_divide(a, b) toh
agar b == 0 toh
gopgop "CRITICAL: Attempted to divide by zero!"
wapas a / b
// Outer runner function implementing try/catch handler
kaam run_math() toh
laadle result hai 0
koshish toh
// Success case
bol "10 / 2 = " + safe_divide(10, 2)
// This line fails and forces VM to jump immediately
result hai safe_divide(10, 0)
// The panic jumped over this line, so it never prints
bol "You will never see me!"
pakad err toh
bol "Math error caught successfully!"
bol err
bol "The VM is still alive and running safely!"
run_math()What's Happening?
- The Throw (
gopgop): Insidesafe_divide, checkingb == 0intercepts math faults and actively throws a text literal instead of triggering a Rust core panic during VM execution. - The Binding (
koshish): Ourrun_mathfunction wraps the dangerous division inside akoshishblock. Behind the scenes, the Compiler issues a silentPushErrHandleropcode right before the block starts, taking a snapshot of the Call Stack depth and memory registers. - The Unwind (
pakad err toh): When the division code triggers thegopgop, execution immediately halts. The VM jumps all the way back acrossCallFrames, binds the string "CRITICAL: Attempted to divide by zero!" into theerrvariable, and then resumes executing the handler. - Clean Exit: Because the error is completely handled locally,
The VM is still alive and running safely!simply prints at the end, and the program exits successfully.