How are PLTScheme Catch errors?

The "error" function in PLTScheme strikes me. If I have division by zero, it does not perform any other recursion and just exits the call stack and gives me an error.

Is there an implicit continuation before all functions? Does the error throw a call stack? Does anyone know about this?

+5
source share
1 answer

In a PLT diagram, a procedure error throws an exn: fail exception, which contains an error string. There is no “implicit catch” for all definitions. Take a look at the following example:

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

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

Run the above script from the command line and you will see that the interpreter exists after printing something like

/: division by zero

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

, , , .. . , : " , , , ". , JVM - .

PLT Scheme, MzScheme Language Manual. , try-catch-finally Java.

(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 ...)))))
+6

All Articles