lips icon indicating copy to clipboard operation
lips copied to clipboard

try..catch is broken

Open jcubic opened this issue 4 years ago • 4 comments

Here is an example:

(define (until-zero fn lst)
  (try (for-each (lambda (x)
                   (if (zero? x)
                       (throw 'ZONK)
                       (fn x)))
                 lst)
       (catch (e)
              (print "err")
              (print e))))

(print "----")
(until-zero print '(1 2 0 3 4))
(print "----")
(until-zero print '(0 1 2 3 4))

it prints:

----
1
2
err
ZONK
3
4
----
err
ZONK
1
2
3
4

another issue is that when throws is called it keep adding Error: so last error is:

Error: Error: Error: Error: ZONK

jcubic avatar Aug 14 '21 18:08 jcubic

This is a fundametal error, exceptions don't stop the execution:

(try
 (begin
   (print 'foo)
   (print 'bar)
   (throw 'ZONK)
   (print 'baz)
   (print 'quux))
 (catch (e)
        (print e)))
;; ==> foo
;; ==> bar
;; ==> ZONK
;; ==> baz
;; ==> quux

Probably because if real exceptions happen it's bubble up in evaluate. Maybe silent.error needs to be introduced like in R shiny framework. Or we need to wait for continuations.

jcubic avatar Aug 14 '21 18:08 jcubic

Another idea is to create a stack of error handlers and handle errors by hand instead of using functions. Maybe try..catch needs to be in the core of evaluate (but It would be nice if this could be just macro).

jcubic avatar Aug 14 '21 18:08 jcubic

Does Lips implement (guard ...) from R7RS?

lassik avatar Aug 15 '21 07:08 lassik

@lassik No, but It probably can be implemented with try..catch, it will be simple symbol swapping. I think that this is a good time to start adding continuations.

jcubic avatar Aug 15 '21 07:08 jcubic

Basic code works:

(try
 (begin
   (print 'foo)
   (print 'bar)
   (throw 'ZONK)
   (print 'baz)
   (print 'quux))
 (catch (e)
        (print e)))

But the for-each example is still not fixed.

jcubic avatar Jan 19 '24 18:01 jcubic