Error Handling

Error Handling in Thorn

Thorn uses risk blocks. Errors become first-class values rather than exceptions that unwind the stack invisibly.


Basic risk

risk {
    # risky code here
}

Capturing the error

err = risk {
    res = reach(url)
    send res.json
}
if (err != void) {
    write(v"failed: {err}")
}

Custom Error Prefix

risk("network error: ") {
    # risky code here
}

Handler on Failure

task myHandler() {
    write("something went wrong")
}
risk(myHandler) {
    # risky code here
}

try / catch

try {
    # risky code
} catch(e) {
    write(e)
}

Use risk when you want errors as values; use try/catch for traditional exception handling.

Language Reference: Errors →