exception handling in MIT scheme

1.5k views Asked by At

How do I raise and handle an exception in MIT scheme?

Something like [it doesn't work]

((< val 0) (raise "-ve value") )
2

There are 2 answers

1
dvingo On

The documentation does provide the answer, but no code samples, so here is one:

(define (handler x)
    (display "Handling Error: ")(display x)(newline)
    (restart 1))

Here we're just displaying the error (what the documentation calls a "condition") and doing nothing.

To have this function handle all conditions do:

(bind-default-condition-handler '() handler)

Or you can just wrap one code block with:

(bind-condition-handler '() handler (3 4))
0
user1526533 On

As dvingo pointed out, the docs do not show any example so here is another example that uses the "error" built-in special form (at least in MIT-scheme):

(define (errors-if-zero x)
  (if (= x 0)
    (error "x is 0")
    x))