AtlatestRepositorysigil-web-styles
sigil-web-styles / tree / build / dev / lib / _pkg / sigil-stdlib / languageexceptions.md
2
# Exceptions4
> Error handling with guard, raise, and exception types.6
Note: Exception handling requires `(import (sigil error))`.8
## guard10
Handle exceptions with pattern-like clauses. The primary way to catch errors.12
```scheme13
(import (sigil error))15
(guard (err16
((type-error? err)17
(display "Type error: ")18
(display (exception-message err))19
#f)20
((error? err)21
(display "Error occurred")22
#f)23
(else24
(raise err))) ; Re-raise if unhandled25
(risky-operation))26
```28
### Syntax30
```scheme31
(guard (variable32
(test expression ...)33
(test expression ...)34
(else expression ...))35
body ...)36
```38
Each clause tests the caught exception. If a test succeeds, its expressions are evaluated and the result returned. The `else` clause catches anything.40
### Without else42
If no clause matches and there's no `else`, the exception propagates.44
```scheme45
(guard (e ((eq? (exception-kind e) 'expected) 'handled))46
(raise (make-exception 'unexpected "oops" '())))47
; Exception propagates (not caught)48
```50
## raise52
Signal an exception. Transfers control to the nearest handler.54
```scheme55
(raise (make-exception 'my-error "something went wrong" '()))56
```58
## error60
Convenient way to signal an error with a formatted message.62
```scheme63
(error "Something went wrong")64
(error "Invalid argument: ~a" x)65
(error "Expected ~a, got ~a" expected actual)66
```68
The message uses `format`-style placeholders:69
- `~a` - Display representation70
- `~s` - Write representation (quoted strings)72
## Exception Types74
Sigil provides typed exceptions via `(sigil error)`:76
```scheme77
(import (sigil error))79
;; Type predicates80
(exception? e) ; Any exception81
(error? e) ; General error82
(type-error? e) ; Type mismatch83
(range-error? e) ; Value out of range84
(arity-error? e) ; Wrong argument count86
;; Low-level exception constructors87
(make-exception 'kind "message" irritants)88
(make-error "message")90
;; Struct error constructors (preferred)91
(type-error message: "msg" expected-type: "pair" procedure-name: "car")92
(arity-error message: "msg" expected-count: 2 got-count: 3)93
(range-error message: "msg" index: 10 min-bound: 0 max-bound: 5)94
(unbound-error message: "msg" variable-name: "x")95
(io-error message: "msg" path: "/tmp/foo" operation: "open")96
```98
### Exception Fields100
```scheme101
(exception-kind e) ; Symbol identifying error type102
(exception-message e) ; Human-readable message103
(exception-irritants e) ; Related values (list)104
(exception-stack-trace e) ; Stack trace if available105
```107
## make-exception109
Create a custom exception.111
```scheme112
(make-exception 'not-found "Resource not found" (list path))113
```115
Arguments:116
1. Kind (symbol) - identifies the exception type117
2. Message (string) - human-readable description118
3. Irritants (list) - related values for debugging120
## Common Patterns122
### Safe Operations124
```scheme125
(define (safe-divide a b)126
(guard (e (else #f))127
(/ a b)))129
(safe-divide 10 2) ; => 5130
(safe-divide 10 0) ; => #f131
```133
### Default on Error135
```scheme136
(define (read-config-or-default path)137
(guard (e ((error? e) (default-config)))138
(read-config path)))139
```141
### Re-raise After Logging143
```scheme144
(define (with-error-logging thunk)145
(guard (e (else146
(display "Error: ")147
(display (exception-message e))148
(newline)149
(raise e))) ; Re-raise after logging150
(thunk)))151
```153
## Stack Traces155
When an unhandled exception reaches the REPL, Sigil prints a stack trace showing where the error occurred.157
In code, access via `(exception-stack-trace e)` when available.159
## with-exception-handler161
Low-level handler installation. Rarely needed directly—prefer `guard`.163
```scheme164
(with-exception-handler165
(lambda (e)166
(display "caught!")167
(raise e)) ; Must re-raise or return168
(lambda ()169
(risky-operation)))170
```172
The handler must either:173
- Raise the exception (or a different one)174
- Return a value (only for continuable exceptions)