PLTScheme 如何捕获错误?

发布于 2024-08-07 18:25:35 字数 120 浏览 7 评论 0 原文

我对 PLTScheme 中的“错误”功能感到惊讶。 如果我除以零,它不会执行任何其他递归,只是从调用堆栈中出来并给我一个错误。

所有函数之前是否都有隐式延续?该错误是否会丢弃调用堆栈?有人对此有任何想法吗?

I am amazed by the "error" function in PLTScheme.
If I have a division by zero, it doesnt do any other recursion and just comes out of the call stack and give me an error.

Is there an implicit continuation before all the functions? Does the error throw away the call stack? Does anybody have any idea about this?

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

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

发布评论

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

评论(1

痴情 2024-08-14 18:25:35

在 PLT 方案中,过程 error 引发异常 exn:fail,其中包含错误字符串。所有定义都没有“隐式捕获”。看下面的示例:

;; test.ss
(define (a d)
  (printf "~a~n" (/ 10 d)))

(a 0) ;; The interpreter will exit here.     
(printf "OK~n")

从命令行执行上述脚本,您将在打印类似内容后看到解释器存在

/: division by zero

 === context ===
/home/user/test.ss:1:0: a

如果用户程序中未处理异常,它将传播到默认处理程序处理的核心解释器它,即打印异常并退出。换句话说,口译员只是说:“出现了异常,我不知道如何处理,所以我退出”。这与 JVM 或其他一些虚拟机处理异常的方式没有太大区别。

要了解有关 PLT Scheme 异常处理机制的更多信息,请阅读 with-handlers 和 dynamic-wind .edu/CS/PLT/packages/100/pdf/mzscheme.pdf" rel="nofollow noreferrer">MzScheme 语言手册。使用它们,您甚至可以模拟 Java 的 try-catch-finally 块。

(define (d a b)
  (try
   (printf "~a~n" (/ a b))
   (catch (lambda (ex)
            (printf "Error: ~a" ex)))
   (finally 
    (if (> b -2) 
      (d a (sub1 b))))))

这是使上述成为可能的语法扩展:

;; try-catch-finally on top of with-handlers and dynamic-wind.

(define-syntax try
  (syntax-rules (catch finally)
    ((_ try-body ... (catch catch-proc))
     (with-handlers (((lambda (ex) #t)
              (lambda (ex) 
            (catch-proc ex))))
            (begin
              try-body ...)))
    ((_ try-body ... (catch catch-proc) (finally fin-body ...))
     (dynamic-wind
     (lambda () ())

     (lambda ()
       (with-handlers (((lambda (ex) #t)
                (lambda (ex) 
                  (catch-proc ex))))
              (begin
                try-body ...)))

     (lambda () fin-body ...)))
    ((_ try-body ... (finally fin-body ...))
     (dynamic-wind
     (lambda () ())

     (lambda () try-body ...)

     (lambda () fin-body ...)))))

In PLT Scheme, the procedure error raises the exception exn:fail, which contains an error string. There is no "implicit catch" for all defines. Look at the following sample:

;; test.ss
(define (a d)
  (printf "~a~n" (/ 10 d)))

(a 0) ;; The interpreter will exit here.     
(printf "OK~n")

Execute the above script from the command line and you will see the interpreter existing after printing something like

/: division by zero

 === context ===
/home/user/test.ss:1:0: a

If an exception is not handled within the user program, it is propagated up to the core interpreter where a default handler deals with it, i.e print the exception and exit. In other words, the interpreter just says, "an exception was raised and I don't know how to deal with it, so I am quiting". This is not much different from how the JVM or some other virtual machine handle exceptions.

To learn more about PLT Scheme's exception handling mechanism, please read about with-handlers and dynamic-wind in the MzScheme Language Manual. Using these, you can even emulate Java's try-catch-finally block.

(define (d a b)
  (try
   (printf "~a~n" (/ a b))
   (catch (lambda (ex)
            (printf "Error: ~a" ex)))
   (finally 
    (if (> b -2) 
      (d a (sub1 b))))))

Here is the syntax extension that made the above possible:

;; try-catch-finally on top of with-handlers and dynamic-wind.

(define-syntax try
  (syntax-rules (catch finally)
    ((_ try-body ... (catch catch-proc))
     (with-handlers (((lambda (ex) #t)
              (lambda (ex) 
            (catch-proc ex))))
            (begin
              try-body ...)))
    ((_ try-body ... (catch catch-proc) (finally fin-body ...))
     (dynamic-wind
     (lambda () ())

     (lambda ()
       (with-handlers (((lambda (ex) #t)
                (lambda (ex) 
                  (catch-proc ex))))
              (begin
                try-body ...)))

     (lambda () fin-body ...)))
    ((_ try-body ... (finally fin-body ...))
     (dynamic-wind
     (lambda () ())

     (lambda () try-body ...)

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