Error Handling

Recover from runtime failures with try/catch and inspect error codes and messages.

Error Handling

Check the program before running it. Invalid syntax, unknown names, and mismatched arguments must be fixed in source; try/catch does not handle those diagnostics.

Catch an operation failure

Use try/catch around work that may fail, such as a request timing out. Put it inside a loop to keep a single failed request from stopping the other items.

input endpoints: list<url>
for endpoint in endpoints {
    try {
        response = http.get(endpoint, timeout=3s)
        expect response.status == 200, "Unexpected HTTP status"
    } catch error {
        print(`${error.code}: ${error.message}`)
    }
}

The caught error has code and message fields. Codes depend on the operation; use the message for context. You can omit the variable with catch { ... } when you do not need its details.

An uncaught runtime failure stops the run. Catch expected operation failures where you can recover; do not treat catch as protection against every failure or run limit.

Failed checks are different

A false expect records a failed check, or a finding when severity is supplied, and continues. It does not enter catch merely because the condition is false. Evaluating the condition or making the preceding request can still fail.

enesfrdenbsvfiplarhe