[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: exceptions

Here's a transliteration of your program, pasted from the DrScheme
definitions window.

Hope that helps!


;; divider : number -> number -> number
(define (divider x)
  (lambda (y)
    (/ y x)))

;; halver : number -> number
(define halver (divider 2))

(halver 9) ;; fractions are the default :)

(halver 4)

(define bomber (divider 0))

; (bomber 4) ;; uncomment to see error

;; a with-handlers expression lets you specify
;; many handlers simulatenously. In this case
;; there is only one, bracketed by []
;; Each handler consists of a predicate for
;; recognizing the exceptions and a function 
;; for handling the exception. Both are passed
;; the exception record.
(with-handlers ([(lambda (exn) #t)
                 (lambda (exn)
                   (printf "you moron -- something went wrong~n"))])
  (bomber 4))

;; the above is actually bad practice --
;; you shouldn't handle the break exception
;; unless you really mean to stop the `break'
;; button or you are using breaks for some other
;; purpose. Use the builtin `not-break-exn'
;; instead.
(with-handlers ([not-break-exn?
                 (lambda (x)
                   (printf "you moron -- something went wrong~n"))])
  (bomber 4))

;; mzscheme also supports various classes of exceptions, 
;; search for `primitive exceptions' in Help Desk to
;; see the gory details.