Error Handling
Astra catches runtime problems with a structured when / thenblock, so a single bad line doesn't have to crash your whole program. You match on the specific error type and decide what happens next.
Why error handling
Without a when block, a runtime error stops execution and prints a diagnostic with the line number and error type:
when lets you recover instead.when / then syntax
Put the risky code inside when:, and handle a specific failure with then ERROR_TYPE:. Close the whole block with ;.
Output:
The program kept running instead of stopping — x was never defined, Astra raised UNDEFINED_VAR, and the matching then block ran.
Catching multiple error types
Add as many then ERROR_TYPE: clauses as you need — each one only runs for its matching error:
Output:
x is defined this time, so write x succeeds and prints 10. The next line tries to add a boolean to a number, which raises INVALID_OPERATION — only that clause runs, the rest of the when block stops there.
Fallback then
A plain then:with no error type catches anything not matched by an earlier clause — useful as a catch-all so unexpected errors don't crash the program:
then: last so it only fires when nothing else matched.Common error types
| Error type | Raised when |
|---|---|
| UNDEFINED_VAR | A variable is used before it's declared. |
| INVALID_OPERATION | An operation mixes incompatible types, like adding a boolean to a number. |
| TYPE_MISMATCH | A value doesn't match the type a function or chain field expects. |
| INDEX_OUT_OF_RANGE | A chain is accessed at an index that doesn't exist. |
INFINITE and keeps running. See Getting Started for that example.