Skip to content

Instantly share code, notes, and snippets.

@tkurtbond
Last active December 24, 2021 15:20
Show Gist options
  • Save tkurtbond/278f14517582203e1930b77532fc472e to your computer and use it in GitHub Desktop.
Save tkurtbond/278f14517582203e1930b77532fc472e to your computer and use it in GitHub Desktop.
;;;; -*- geiser-scheme-implementation: chicken -*-
;;;; Look at https://wiki.call-cc.org/man/5/Module%20(chicken%20condition)#system-conditions
;;;; for a list of the CHICKEN Scheme system exceptions.
;;;; The original of the example I changed for the gist is in the
;;;; documentation of the module (chicken conditions), which I changed
;;;; to use procedures from the egg condition-utils to find out more
;;;; detail about the error.
(import format)
(import (chicken port))
(import (chicken tcp))
(import (exn-condition))
(define (check thunk)
(condition-case (thunk)
;; Handle specifically exception arithmetic.
[(exn arithmetic) ; exception (exn arithmetic), not bound to variable
(format #t "arithmetic error~%")]
[ex (exn file) ; exception (exn i/o file), bound to variable ex
;; Note that i/o was not specified in the (exn ...) part, just file.
(format #t "file error: ex: ~s~%~4tmsg: ~s~%~4targs: ~s~%"
ex (exn-message ex) (exn-arguments ex))]
[ex (exn i/o) ; exception (exn i/o), bound to variable ex
;; Note that neither file nor net were specified in the (exn ...) part.
(format #t "i/o error: ex: ~s~%~4tmsg: ~s~%~4targs: ~s~%"
ex (exn-message ex) (exn-arguments ex))]
[ex (exn) ; any exception bound to variable ex
(format #t "other error: ex: ~s~%~4tmsg: ~s~%~4targs: ~s~%"
ex (exn-message ex) (exn-arguments ex))]
[ex () (format #t "something else: ex: ~s~%" ex)]))
(check (lambda () (/ 1 0)))
;; arithmetic error
(check (lambda () (eqv? 1)))
;; other error: ex: #<condition: (exn arity)>
;; msg: "bad argument count - received 1 but expected 2"
;; args: (#<procedure (scheme#eqv? x y)>)
(check (lambda () (call-with-input-string "(" (lambda (p) (read p)))))
;; other error: ex: #<condition: (exn syntax)>
;; msg: "unterminated list"
;; args: ()
(check (lambda () (call-with-input-file "." (lambda (port) (read port)))))
;; file error: ex: #<condition: (exn i/o file)>
;; msg: "cannot read from port - Is a directory"
;; args: (#<input port ".">)
(check (lambda () (tcp-connect "localhost" 8989)))
;; i/o error: ex: #<condition: (exn i/o net)>
;; msg: "cannot create socket - Connection refused"
;; args: ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment