R6RS方案中如何抛出和处理异常

发布于 2024-08-26 05:44:11 字数 227 浏览 1 评论 0原文

R6RS 方案中抛出和捕获异常的标准方法是什么?我正在寻找适用于实现 R6RS 的任何版本的Scheme(不仅仅是PLT)的语法。

R6RS 保护语法 看起来像它可能符合要求,但有人可以向我展示如何实际使用它的示例吗?

What is the standard way to throw and catch exceptions in R6RS Scheme? I'm looking for syntax that works in any version of Scheme (not just PLT) that implements R6RS.

R6RS guard syntax looks like it might fit the bill, but can somebody show me an example of how to actually use it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走干脆点 2024-09-02 05:44:11

guard 的语义是:

(guard (exception-object
       ((condition-1-to-test-exception-object) (action-to-take)
       ((condition-2-to-test-exception-object) (action-to-take)   
       ((condition-N-to-test-exception-object) (action-to-take)
       (else (action-for-unknown-exception)))

有一个辅助的 else 子句,我们在这里不使用。以下示例模拟典型文件 IO 操作可能引发的异常。我们安装一个guard来处理异常:

(define mode 0)

(define (open-file) 
  (if (= mode 1)
      (raise 'file-open-error)
      (display "file opened\n")))

(define (read-file)
  (if (= mode 2)
      (raise 'file-read-error)
      (display "file read\n")))

(define (close-file)
  (if (= mode 3)
      (raise 'file-close-error)
      (display "file closed\n")))

(define (update-mode)
  (if (< mode 3)
      (set! mode (+ mode 1))
      (set! mode 0)))

(define (file-operations)
  (open-file)
  (read-file)
  (close-file)
  (update-mode))

(define (guard-demo)
  (guard (ex 
          ((eq? ex 'file-open-error) 
           (display "error: failed to open file ") 
           (update-mode))
          ((eq? ex 'file-read-error) 
           (display "error: failed to read file ") 
           (update-mode))
          (else (display "Unknown error") (update-mode)))
         (file-operations)))

测试运行:

> (guard-demo)
file opened
file read
file closed
> (guard-demo)
error: failed to open file 
> (guard-demo)
file opened
error: failed to read file 
> (guard-demo)
file opened
file read
Unknown error
> (guard-demo)
file opened
file read
file closed

R6RS 第 7 章

The semantics of guard is:

(guard (exception-object
       ((condition-1-to-test-exception-object) (action-to-take)
       ((condition-2-to-test-exception-object) (action-to-take)   
       ((condition-N-to-test-exception-object) (action-to-take)
       (else (action-for-unknown-exception)))

There is an auxiliary else clause that we do not use here. The following sample simulates exceptions that could be raised by typical file IO operations. We install a guard to handle the exceptions:

(define mode 0)

(define (open-file) 
  (if (= mode 1)
      (raise 'file-open-error)
      (display "file opened\n")))

(define (read-file)
  (if (= mode 2)
      (raise 'file-read-error)
      (display "file read\n")))

(define (close-file)
  (if (= mode 3)
      (raise 'file-close-error)
      (display "file closed\n")))

(define (update-mode)
  (if (< mode 3)
      (set! mode (+ mode 1))
      (set! mode 0)))

(define (file-operations)
  (open-file)
  (read-file)
  (close-file)
  (update-mode))

(define (guard-demo)
  (guard (ex 
          ((eq? ex 'file-open-error) 
           (display "error: failed to open file ") 
           (update-mode))
          ((eq? ex 'file-read-error) 
           (display "error: failed to read file ") 
           (update-mode))
          (else (display "Unknown error") (update-mode)))
         (file-operations)))

Test run:

> (guard-demo)
file opened
file read
file closed
> (guard-demo)
error: failed to open file 
> (guard-demo)
file opened
error: failed to read file 
> (guard-demo)
file opened
file read
Unknown error
> (guard-demo)
file opened
file read
file closed

There is a detailed description of exception handling with example code in Chapter 7 of R6RS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文