Exception
Raise an error
Section titled “Raise an error”Given a file named “main.scm” with:
(import (scheme base))
(error "Oh, no!")
When I run stak main.scm
Then the stderr should contain “Oh, no!”
And the exit status should not be 0.
Raise an error with a value
Section titled “Raise an error with a value”Given a file named “main.scm” with:
(import (scheme base))
(error "Oh, no!" 42)
When I run stak main.scm
Then the stderr should contain “Oh, no!”
And the exit status should not be 0.
Raise an error with a pretty-printed value
Section titled “Raise an error with a pretty-printed value”Given a file named “main.scm” with:
(import (scheme base) (scheme write))
(error "Oh, no!" 42)
When I run stak main.scm
Then the stderr should contain “Oh, no!”
And the stderr should contain “42”
And the exit status should not be 0.
Halt execution on an exception
Section titled “Halt execution on an exception”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 65)
(error "")
(write-u8 66)
When I run stak main.scm
Then the exit status should not be 0
And the stdout should contain exactly “A”.
Raise a non-continuable exception
Section titled “Raise a non-continuable exception”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (with-exception-handler (lambda (value) 65) (lambda () (raise #f))))
When I run stak main.scm
Then the exit status should not be 0
And the stdout should contain exactly "".
Raise a continuable exception
Section titled “Raise a continuable exception”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (with-exception-handler (lambda (value) 65) (lambda () (raise-continuable #f))))
When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Raise an exception in a handler
Section titled “Raise an exception in a handler”Given a file named “main.scm” with:
(import (scheme base))
(with-exception-handler (lambda (value) (raise #f)) (lambda () (raise-continuable #f)))
When I run stak main.scm
Then the exit status should not be 0.
Raise an exception in nested handlers
Section titled “Raise an exception in nested handlers”Given a file named “main.scm” with:
(import (scheme base) (scheme write))
(with-exception-handler (lambda (value) (error "foo")) (lambda () (with-exception-handler (lambda (value) (display "bar") (raise #f)) (lambda () (raise #f)))))
When I run stak main.scm
Then the exit status should not be 0
And the stderr should contain “foo”
And the stdout should contain “bar”.
Raise an exception in deeply nested handlers
Section titled “Raise an exception in deeply nested handlers”Given a file named “main.scm” with:
(import (scheme base) (scheme write))
(with-exception-handler (lambda (value) (error "foo")) (lambda () (with-exception-handler (lambda (value) (display "bar") (raise #f)) (lambda () (with-exception-handler (lambda (value) (display "baz") (raise #f)) (lambda () (raise #f)))))))
When I run stak main.scm
Then the exit status should not be 0
And the stderr should contain “foo”
And the stdout should contain “bar”
And the stdout should contain “baz”.
Terminate on a continued non-continuable exception
Section titled “Terminate on a continued non-continuable exception”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (with-exception-handler (lambda (value) 65) (lambda () (raise #f))))
When I run stak main.scm
Then the exit status should not be 0
And the stdout should contain exactly "".
Terminate on a continued non-continuable exception with a proper message
Section titled “Terminate on a continued non-continuable exception with a proper message”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (with-exception-handler (lambda (value) 65) (lambda () (raise #f))))
When I run stak main.scm
Then the exit status should not be 0
And the stdout should contain exactly ""
And the stderr should contain “exception handler returned on non-continuable exception”.
Leave a dynamic extent
Section titled “Leave a dynamic extent”Given a file named “main.scm” with:
(import (scheme base))
(dynamic-wind (lambda () (write-u8 65)) (lambda () (error "")) (lambda () (write-u8 66)))
When I run stak main.scm
Then the exit status should not be 0
And the stdout should contain exactly “AB”.
Use a guard
expression
Section titled “Use a guard expression”Given a file named “main.scm” with:
(import (scheme base))
(guard (condition ((null? condition) #f)
((number? condition) (write-u8 condition))
((string? condition) #f)) (raise 65))
When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Use an else
clause in a guard expression
Section titled “Use an else clause in a guard expression”Given a file named “main.scm” with:
(import (scheme base))
(guard (condition ((null? condition) #f)
((string? condition) #f)
(else (write-u8 condition))) (raise 65))
When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Use nested guard
expressions
Section titled “Use nested guard expressions”Given a file named “main.scm” with:
(import (scheme base))
(guard (condition ((null? condition) #f)
((number? condition) (write-u8 condition))
((string? condition) #f)) (guard (condition ((null? condition) #f)
((number? condition) (write-u8 condition))
((string? condition) #f)) (raise 65)))
When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Use a guard
expression in a clause
Section titled “Use a guard expression in a clause”Given a file named “main.scm” with:
(import (scheme base))
(guard (condition ((null? condition) #f)
((number? condition) (guard (condition ((null? condition) #f)
((number? condition) (write-u8 condition))
((string? condition) #f)) (raise condition)))
((string? condition) #f)) (raise 65))
When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Return from a body in a guard
expression
Section titled “Return from a body in a guard expression”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (guard (value (else 66)) 65))
When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Return from an else
clause in a guard
expression
Section titled “Return from an else clause in a guard expression”Given a file named “main.scm” with:
(import (scheme base))
(write-u8 (guard (value (else 65)) (raise #f)))
When I successfully run stak main.scm
Then the stdout should contain exactly “A”.
Catch a runtime error
Section titled “Catch a runtime error”Given a file named “main.scm” with:
(import (scheme base) (scheme process-context) (stak base))
(define (foo x) x)
(with-exception-handler (lambda (error) (write-string (error-object-message error)) (exit 1)) (lambda () <expression>))
When I run stak main.scm
Then the stdout should contain exactly “<message>”
And the exit status should not be 0.
Examples
Section titled “Examples”expression | message |
---|---|
((lambda (x) x)) | invalid argument count |
((primitive 4242)) | illegal primitive |
(#f) | procedure expected |