summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorEli Barzilay <eli@barzilay.org>2013-07-16 03:32:03 -0400
committerEli Barzilay <eli@barzilay.org>2013-07-16 03:35:12 -0400
commitc11ec1f137eb4498818516ab408edb3cfae8317e (patch)
tree81a4c04ba33f2a3ddd49b7f6baf4499b0eaf513e
parent8a065aa2562f55acf3f676b14d208ebbf86e7649 (diff)
Improve `with-handlers' an exceptions description.
-rw-r--r--racket.html.markdown17
1 files changed, 11 insertions, 6 deletions
diff --git a/racket.html.markdown b/racket.html.markdown
index d3403ff8..4b34fbef 100644
--- a/racket.html.markdown
+++ b/racket.html.markdown
@@ -375,12 +375,17 @@ m ; => '#hash((b . 2) (a . 1) (c . 3)) <-- no `d'
;;; Exceptions
-;; To catch an exception, use the `with-handlers' form
-;; To throw an exception use `raise'
-(with-handlers
- ([(lambda (v) (equal? v "infinity"))
- (lambda (exn) +inf.0)])
- (raise "infinity"))
+;; To catch exceptions, use the `with-handlers' form
+(with-handlers ([exn:fail? (lambda (exn) 999)])
+ (+ 1 "2")) ; => 999
+(with-handlers ([exn:break? (lambda (exn) "no time")])
+ (sleep 3)
+ "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