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:

whentest.astra
write x
terminal
[ASTRA-RUN-ERR] :: Line 1 :: UNDEFINED_VAR: x. (Check if the variable is declared)
⚠️ Without handling: the program exits immediately at the failing line. Wrapping it in 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 ;.

whentest.astra
when: write x then UNDEFINED_VAR: write "varaible not found" ;

Output:

varaible not found

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:

whentest.astra
when: x = 10 write x write x + TRUE then UNDEFINED_VAR: write "varaible not found" then INVALID_OPERATION: write "Boolean not in arithmatic" ;

Output:

10 Boolean not in arithmatic

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:

fallback.astra
when: write undefined_var then UNDEFINED_VAR: write "Variable not found!" then: write "Something went wrong" ;
💡 Tip: Order matters. List specific error types first, and put the fallback then: last so it only fires when nothing else matched.

Common error types

Error typeRaised when
UNDEFINED_VARA variable is used before it's declared.
INVALID_OPERATIONAn operation mixes incompatible types, like adding a boolean to a number.
TYPE_MISMATCHA value doesn't match the type a function or chain field expects.
INDEX_OUT_OF_RANGEA chain is accessed at an index that doesn't exist.
ℹ️ Note:Dividing by zero doesn't raise an error at all — it returns INFINITE and keeps running. See Getting Started for that example.