try-catch
The try-catch expression allows you to handle errors gracefully by providing a fallback value when an expression fails.
Syntax
try <expr> catch <expr>
Behavior
- If the
tryexpression succeeds, its result is returned - If the
tryexpression fails (produces an error), thecatchexpression is evaluated instead - The
catchexpression receives the same input as thetryexpression
Examples
Basic Error Handling
# When the expression succeeds
try: "value" catch: "unknown"
# When the expression fails
try: get("missing") catch: "default"
Chaining with Pipe
# Try to parse as JSON, fallback to raw string
try: from_json() catch: self
# Complex fallback logic
try: do get("data") | from_json(); catch: []
Nested Try-Catch
# Multiple fallback levels
try: get("primary") catch: try: get("secondary") catch: "default"
Error Suppression (?)
The error suppression operator ? provides a concise way to handle errors by returning None when an expression fails, instead of raising an error. This is equivalent to using a regular try-catch with a default fallback.
Examples
# Equivalent to a regular try-catch with a default value
get("missing")?
In this example, if get("missing") fails, the result will be None rather than an error.