Skip to content

Panics and Exceptions

The following error types exist right now within the core:

std::Error

Standard error types for the Helix language

Classes

BaseError

Base class for all Helix errors

NullValueError

Error thrown when a null value is encountered where a valid value is expected

RuntimeError

Error thrown for general runtime errors

StateMismatchError

Error thrown when an object is in an incorrect state for the requested operation

TypeMismatchError

Error thrown when a type mismatch occurs

you can either panic like this:

fn do_something(value: i32) -> i32 { /// since we are not returning an `i32?` this will throw
panic std::Error::RuntimeError("This is a runtime error panic!");
}
// or we can return the error
fn do_something_else(value: i32) -> i32? { /// since we are returning an `i32?` we can return null on error
return std::Error::RuntimeError("This is a runtime error return!");
}
// read the error/handle it
fn main() {
// handling throw
try {
do_something(5); // this will throw and stop execution
} catch (err: std::Error) {
std::print(f"Caught an error from do_something: {err}");
}
// handling return
let result: i32? = do_something_else(10);
if (result == null) {
std::print("do_something_else returned null");
} else if (std::Error::RuntimeError in result) {
std::print(f"Caught an error from do_something_else");
} else {
std::print(f"do_something_else returned: {*result}");
}
// remember to collapse quantom types when using them.
}

If you do want to explictly crash (like in the main funciton where a panic is not allowed):

fn main() {
std::crash(std::Error::RuntimeError("This is a fatal runtime error crash!"));
}

This will immediately terminate the program and print the error message.