Arc Forumnew | comments | leaders | submitlogin
6 points by almkglor 6046 days ago | link | parent

Hmm. So another priority: a decent error system.

I've been wondering about decent error/exception handling recently. One particular bit I noticed in C++ is that throwing is something like this:

  class MyExceptionType : std::exception {
    /*blah blah...*/
  }
  void random_function(void){
    /*blah blah*/
    throw(MyExceptionType(my_specific_reason));
  }
To me this seems like an Arcish way to do it:

  (def random-function ()
    ; blah blah
    (err:annotate 'MyExceptionType my-specific-reason))
Any special exception-handling would look like this:

  (on-err
    (fn ()
      #|do stuff...|#)
    (fn (e)
      (if (isa e 'MyExceptionType)
          (let reason (rep e)
            #|handle according to reason|#)
          ; unknown error, rethrow:
          (err e))))
In fact, I have a draft implementation of 'on-err and 'err for arc2c:

  (set on-err
    (fn (f fh)
      (ccc
        (fn (k)
          ; let tmp (%curr-err)
          ((fn (tmp)
             ; set up a continuation guard
             (%cont-guard-up)
             (%set-err
               (fn (e)
                 ; tear down continuation guard
                 ; and current error handler
                 (%cont-guard-down)
                 (%set-err tmp)
                 ; so that if fh throws, it throws
                 ; on the original err instead of
                 ; recursing
                 (k (fh e)) ))
             (f)
             ; tear down continuation guard and
             ; reset error handler
             (%cont-guard-down)
             (%set-err tmp))
            (%curr-err))))))
  (set err
    (fn (e)
      ((%curr-err) e)))
  (%set-err
    (fn (e)
      (%pr "Error of type: ")
      (%prn (%type e))
      (%pr "Error: ")
      (%prn (%rep e))
      (%halt)))
%curr-err and %set-err would have to set a thread-local variable in the C-side. %cont-guard-up and %cont-guard-down just need to set up continuation guards.


1 point by almkglor 6045 days ago | link

Done and on arc2c.git

Continuation guards are not yet implemented though.

Edit: For when macros finally (!!) get into arc2c:

  (mac catch-err (expr . body)
    (givens p (pairs body)
            e (uniq)
            to-isa
            (fn (el)
              (if (no:cdr el)
                  el
                  (let (type exp) el
                    `((isa ,e ,type) ,exp))))
            clauses (mappend to-isa p)
      `(on-err
         (fn () ,expr)
         (fn (,e)
           (if ,@clauses)))))

-----