Skip to main content

Module exceptions

Module exceptions 

Source
Expand description

Exceptional situations and conditions.

Scheme has two distinct concepts: conditions and exceptions. Exceptions are values that values passed to the raise and raise-continuable procedures and can be any Value. Conditions are records that contain information describing an erroneous situation or condition.

Conditions in Scheme are either simple or compound. Scheme-rs provides the ability to inspect conditions without discerning whether they or simple or compound. Using the condition method, a specific condition and thus its associated information can be extracted from the condition.

For example, a common condition is the &trace condition, which can be used to extract a stack trace for the exception:

// Code from scheme-rs repl to print errors:
fn print_exception(exception: Exception) {
    let Ok(conditions) = exception.simple_conditions() else {
        println!(
            "Exception occurred with a non-condition value: {:?}",
            exception.0
        );
        return;
    };
    println!("Uncaught exception:");
    for condition in conditions.into_iter() {
        if let Some(message) = condition.cast_to_rust_type::<Message>() {
            println!(" - Message: {}", message.message);
        } else if let Some(syntax) = condition.cast_to_rust_type::<SyntaxViolation>() {
            println!(" - Syntax error in form: {:?}", syntax.form);
            if let Some(subform) = syntax.subform.as_ref() {
                println!("   (subform: {subform:?})");
            }
        } else if let Some(trace) = condition.cast_to_rust_type::<StackTrace>() {
            println!(" - Stack trace:");
            for (i, trace) in trace.trace.iter().enumerate() {
                let syntax = trace.cast_to_scheme_type::<Gc<Syntax>>().unwrap();
                let span = syntax.span();
                let func_name = syntax.as_ident().unwrap().symbol();
                println!("{:>6}: {func_name}:{span}", i + 1);
            }
        } else {
            println!(" - Condition: {condition:?}");
        }
    }
}

Macros§

define_condition_type
A macro for easily creating new condition types.

Structs§

Assertion
CompoundCondition
Error
Exception
A signal of some sort of erroneous condition.
ImplementationRestriction
ImportError
Irritants
Lexical
Message
NonContinuable
Serious
SimpleCondition
StackTrace
SyntaxViolation
Undefined
Violation
Warning
Who

Functions§

assertion_violation
condition
condition_pred
error
raise
Raises a non-continuable exception to the current exception handler.
raise_builtin
raise_continuable
simple_conditions
with_exception_handler