mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-13 10:14:58 +02:00
Improve `with-handlers' an exceptions description.
This commit is contained in:
@@ -375,12 +375,17 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d'
|
|||||||
|
|
||||||
;;; Exceptions
|
;;; Exceptions
|
||||||
|
|
||||||
;; To catch an exception, use the `with-handlers' form
|
;; To catch exceptions, use the `with-handlers' form
|
||||||
;; To throw an exception use `raise'
|
(with-handlers ([exn:fail? (lambda (exn) 999)])
|
||||||
(with-handlers
|
(+ 1 "2")) ; => 999
|
||||||
([(lambda (v) (equal? v "infinity"))
|
(with-handlers ([exn:break? (lambda (exn) "no time")])
|
||||||
(lambda (exn) +inf.0)])
|
(sleep 3)
|
||||||
(raise "infinity"))
|
"phew") ; => "phew", but if you break it => "no time"
|
||||||
|
|
||||||
|
;; Use `raise' to throw exceptions or any other value
|
||||||
|
(with-handlers ([number? ; catch numeric values raised
|
||||||
|
identity]) ; return them as plain values
|
||||||
|
(+ 1 (raise 2))) ; => 2
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; 6. Mutation
|
;; 6. Mutation
|
||||||
|
Reference in New Issue
Block a user