我想使用 call/cc 来模拟异常处理语句:try...(throw)...exception。这是代码:
(define (month n) ; check if it's a month number (1-12)
(if (or (not (integer? n)) (< n 1) (> n 12))
(throw -1)
(display n)
)
)
(define error (call/cc
(lambda(throw)
(begin
(month 12)
(month -1)
(throw -1) ; won't be executed
(month 10)
(display "Hello world")
)
)
)
)
(if error
(display Error occured!")
)
但是,当我执行它时,它显示了错误(在 biwascheme 中):
Error: execute: unbound symbol: "throw" [(anon), month]
我认为 lambda 中的 throw 与被调用函数“month”中的 throw 不一样,但是,我该如何解决呢?是否可以通过使用一些关键字制作marco来解决?例如:
(define-syntax exception-handling
(syntax-rules (throw raise error)
((_ body catch)
(define (error
(call/cc (lambda (throw) (begin body))))
)
(if error (begin catch)))
)
)