Exception Handling
Source:
tests/language/exception_test.crush
try/catch/throw are fully implemented — not a future feature.
print("Starting Exception Test")
try {
print("Inside try block")
throw "Oops"
print("This should not print")
} catch e {
print("Caught exception: " + e)
}
print("After catch block")
Output:
Starting Exception Test
Inside try block
Caught exception: Oops
After catch block
What this shows:
try { ... } catch e { ... }— the caught value binds toethrow expr— throws any value as an exception (string, Int, Map, etc.)- Execution after
throwinside thetryblock is skipped - Execution after the
catchblock continues normally
The compiler emits enter_try / exit_try / throw CASM instructions.
Defensive Pattern
fn safe_divide(a: Int, b: Int) -> Int {
if b == 0 {
throw "division by zero";
}
return a / b;
}
try {
let result = safe_divide(10, 0);
print(result);
} catch e {
print("Error: " + e);
}