(sigil error)
(sigil error) - Exception Handling
Handle errors gracefully with exceptions. Use guard for structured error handling, create typed exceptions for different error conditions, and format exceptions for user-friendly error messages.
Provides both traditional exception vectors (for raise/guard) and a struct-based error hierarchy for structured access to error details. Use exception->error to convert caught exceptions to rich error structs.
(import (sigil error))
;; Handle errors with guard
(guard (exn
((type-error? exn) "wrong type!")
(else (format "error: ~a" (exception-message exn))))
(risky-operation))
;; Convert to struct for structured access
(guard (exn
(else
(let ((err (exception->error exn)))
(when (type-error? err)
(display (type-error-expected-type err))))))
(car 42))Exports
sigil-errorprocedureBase error type with message and optional stack trace.
sigil-error?procedureTest if a value is a sigil-error struct.
sigil-error-messageprocedureGet the message field of a sigil-error struct.
sigil-error-stack-traceprocedureGet the stack-trace field of a sigil-error struct.
%type-error--typevariableGet the stack-trace field of a sigil-error struct.
type-errorprocedureType mismatch error with procedure name and expected/got types.
(type-error message: "car: expected pair"
procedure-name: "car"
expected-type: "pair")type-error?procedureTest if a value is a type-error struct.
type-error-messageprocedureGet the message field of a type-error struct.
type-error-stack-traceprocedureGet the stack-trace field of a type-error struct.
type-error-procedure-nameprocedureGet the procedure-name field of a type-error struct.
type-error-expected-typeprocedureGet the expected-type field of a type-error struct.
type-error-got-typeprocedureGet the got-type field of a type-error struct.
type-error->sigil-errorprocedureGet the got-type field of a type-error struct.
%arity-error--typevariableGet the got-type field of a type-error struct.
arity-errorprocedureWrong argument count error.
(arity-error message: "foo: expected 2 arguments, got 3"
procedure-name: "foo"
expected-count: 2
got-count: 3)arity-error?procedureTest if a value is a arity-error struct.
arity-error-messageprocedureGet the message field of a arity-error struct.
arity-error-stack-traceprocedureGet the stack-trace field of a arity-error struct.
arity-error-procedure-nameprocedureGet the procedure-name field of a arity-error struct.
arity-error-expected-countprocedureGet the expected-count field of a arity-error struct.
arity-error-got-countprocedureGet the got-count field of a arity-error struct.
arity-error->sigil-errorprocedureGet the got-count field of a arity-error struct.
%unbound-error--typevariableGet the got-count field of a arity-error struct.
unbound-errorprocedureUnbound variable error.
(unbound-error message: "unbound variable 'foo'"
variable-name: "foo")unbound-error?procedureTest if a value is a unbound-error struct.
unbound-error-messageprocedureGet the message field of a unbound-error struct.
unbound-error-stack-traceprocedureGet the stack-trace field of a unbound-error struct.
unbound-error-variable-nameprocedureGet the variable-name field of a unbound-error struct.
unbound-error->sigil-errorprocedureGet the variable-name field of a unbound-error struct.
%range-error--typevariableGet the variable-name field of a unbound-error struct.
range-errorprocedureIndex or value out of range error.
(range-error message: "vector-ref: index 10 out of bounds"
procedure-name: "vector-ref"
index: 10
max-bound: 5)range-error?procedureTest if a value is a range-error struct.
range-error-messageprocedureGet the message field of a range-error struct.
range-error-stack-traceprocedureGet the stack-trace field of a range-error struct.
range-error-procedure-nameprocedureGet the procedure-name field of a range-error struct.
range-error-indexprocedureGet the index field of a range-error struct.
range-error-min-boundprocedureGet the min-bound field of a range-error struct.
range-error-max-boundprocedureGet the max-bound field of a range-error struct.
range-error->sigil-errorprocedureGet the max-bound field of a range-error struct.
%io-error--typevariableGet the max-bound field of a range-error struct.
io-errorprocedureI/O error with path and operation.
(io-error message: "file not found"
path: "/tmp/foo.txt"
operation: "open")io-error?procedureTest if a value is a io-error struct.
io-error-messageprocedureGet the message field of a io-error struct.
io-error-stack-traceprocedureGet the stack-trace field of a io-error struct.
io-error-pathprocedureGet the path field of a io-error struct.
io-error-operationprocedureGet the operation field of a io-error struct.
io-error->sigil-errorprocedureGet the operation field of a io-error struct.
sigil-error?procedureTest if a value is any error (struct or exception).
type-error?procedureTest if a value is a type error (struct or exception).
(guard (exn
((type-error? exn) "type mismatch")
(else "other"))
(car 5))arity-error?procedureTest if a value is an arity error (struct or exception).
(guard (exn
((arity-error? exn) "wrong arg count")
(else "other"))
(apply my-func wrong-args))unbound-error?procedureTest if a value is an unbound variable error (struct or exception).
range-error?procedureTest if a value is a range error (struct or exception).
io-error?procedureTest if a value is an I/O error (struct or exception).
make-errorprocedureCreate a generic error exception.
(raise (make-error "Something went wrong"))
(raise (make-error "Invalid input" input-value))error?procedureTest if an exception is a generic error.
vm-error?procedureTest if an exception is a VM-level error.
VM errors are runtime errors from the Sigil VM that don't map to a more specific error type (type-error, arity-error, etc.).
(guard (exn
((vm-error? exn) "VM error caught")
(else "other error"))
(dangerous-op))exception->errorprocedureConvert an exception (or any raised value) to a rich error struct.
Maps exception kinds to the appropriate struct type, extracting structured fields from the irritants list. Already-converted struct errors are returned as-is.
(guard (exn
(else
(let ((err (exception->error exn)))
(when (type-error? err)
(format "expected ~a" (type-error-expected-type err))))))
(car 42))format-exceptionprocedureFormat an exception as a human-readable string.
Works with both old-style exception vectors and new error structs. Includes the error kind, message, structured fields, and stack trace.
(guard (exn
(else (display (format-exception exn))))
(error "something failed"))
; Error (error): something failedformat-stack-traceprocedureFormat a stack trace as a string.
By default, only shows frames with named functions, hiding anonymous lambdas to reduce noise from macro expansion and internal implementation. Pass #t to show all frames including anonymous ones.
(format-stack-trace (exception-stack-trace exn))
; => "0: my-function\n at main.sgl:42:5\n ..."
(format-stack-trace (exception-stack-trace exn) #t) ; show allprint-exceptionprocedurePrint an exception to standard output.
(guard (exn
(else (print-exception exn)))
(error "failed"))make-exceptionprocedureCreate an exception with the given kind, message, and irritants.
Re-exported from (sigil core)
make-exception-with-traceprocedureCreate an exception with stack trace.
Re-exported from (sigil core)
exception?procedureCheck if obj is an exception.
Re-exported from (sigil core)
exception-kindprocedureGet the kind of an exception (e.g., 'error, 'type-error).
Re-exported from (sigil core)
exception-messageprocedureGet the message from an exception.
Re-exported from (sigil core)
exception-irritantsprocedureGet the irritants (additional data) from an exception.
Re-exported from (sigil core)
exception-stack-traceprocedureGet the stack trace from an exception.
Re-exported from (sigil core)
raiseprocedureRaise an exception (non-continuable). The compiler compiles direct calls to OP_RAISE (which handles stack trace attachment and searches the VM exception handler stack). This Scheme wrapper exists for first-class use (e.g. apply/map).
Re-exported from (sigil core)
raise-continuableprocedureRaise a continuable exception. The handler can return a value to continue execution.
Re-exported from (sigil core)
errorprocedureConvenience function to raise an error exception. (error "message" irritant ...)
Re-exported from (sigil core)
with-exception-handlerprocedureInstall an exception handler and execute thunk (R6RS style). Handler is a procedure taking the exception. Thunk is the body to execute.
Re-exported from (sigil core)
current-exception-handlerprocedureGet the current exception handler.
Re-exported from (sigil core)