Asqan
Asqan

Reputation: 4489

How to implement a try-catch block in scheme?

I'm trying to implement a try-catch block in scheme using (call-cc) method but i'm not sure how it can be used for that. I could not find any example.

And found examples contains just error-handling but what i want to do is: if an error occurred, the scheme program has to give a message to user (via display for-example) without suspending the program.

Is that possible?

Upvotes: 10

Views: 9069

Answers (2)

GoZoner
GoZoner

Reputation: 70155

Since you want to catch all errors, such as ones raised by both raise and raise-continuable you'd need both an exception handler (to deal with raised conditions) and an exit continuation (to avoid continuing with the try body). Simple syntax for try would be:

(import (rnrs base)            ; define-syntax
        (rnrs exceptions))     ; get `with-exception-handler`

(define-syntax try
  (syntax-rules (catch)
    ((_ body (catch catcher))
     (call-with-current-continuation
      (lambda (exit)
        (with-exception-handler
         (lambda (condition)
           catcher
           (exit condition))
         (lambda () body)))))))

This gets used as, for example:

> (try (begin (display "one\n")
              (raise 'some-error)
              (display "two\n"))
    (catch (display "error\n")))
one
error
some-error       # the return value.

Note: this is R6RS (and R7RS) Scheme.

Upvotes: 10

Greg Hendershott
Greg Hendershott

Reputation: 16260

Typically you'd use the with-handlers form. This lets you display an error message or take any other action before returning a value.

#lang racket

(define (foo x)
  (with-handlers ([exn:fail? (lambda (exn)
                               (displayln (exn-message exn))
                               #f)])
    (/ 1 x)))

(foo 1) 
; 1
(foo 0) 
; "/: division by zero" 
; #f

If you really want to use a continuation directly for some reason, you could use call/ec for an error/escape continuation instead of the general call/cc.

Docs:

Upvotes: 12

Related Questions