manpagez: man pages & more
info bigloo
Home | html | info | man
[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

16.2 Exceptions

SRFI-18 function: current-exception-handler

Returns the current exception handler with is a 0-ary procedure.

SRFI-18 function: with-exception-handler handler thunk

Returns the result(s) of calling thunk with no arguments. The handler, which must be a procedure, is installed as the current exception handler in the dynamic environment in effect during the call to thunk. When possible, prefer with-handler to with-exception-handler because the former is more efficient than the latter.

bigloo form: with-handler handler body

Returns the result(s) of evaluating body. The handler, which must be a procedure, is installed as the current exception handler in the dynamic environment in effect during the evaluation of body. Contrarily to with-exception-handler, if an exception is raised, the handler is invoked and the value of the with-handler form is the value produced by invoking the handler.

JVM note: When executed within a JVM, the form with-handler also catches Java exceptions.

The form with-handler is intrinsically more efficient than the function with-exception-handler. From a semantics point of view, with-handler could be re-written in terms of with-exception-handler such as:

 
(with-handler handler1 body1)
≡
(bind-exit (exit)
  (with-exception-handler 
    (lambda (e)
      (exit (handler1 e)))
    (lambda ()
      body)))

It should be noted that the error handler is executed before the execution stack is unwound. Hence, error handlers are executed before protected blocks. For instance in the following code:

 
(with-handler 
   (lambda (e) action)
   (unwind-protect
      body
      protect))

The action is executed before protect.

SRFI-18 function: raise obj

Calls the current exception handler with obj as the single argument. obj may be any Scheme object. Note that invoking the current handler does not escape from the current computation. It is up the to handler to perform the escape. It an error, signaled by the runtime system, if the current exception handler returns.

 
(define (f n)
  (if (< n 0) (raise "negative arg") (sqrt n))))

(define (g)
  (bind-exit (return)
    (with-exception-handler
      (lambda (exc)
        (return
          (if (string? exc)
              (string-append "error: " exc)
              "unknown error")))
      (lambda ()
        (write (f 4.))
        (write (f -1.))
        (write (f 9.))))))

(g)  -| 2. and returns "error: negative arg"

The standard Bigloo runtime system uses the following classes for signaling errors and warnings:


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]
© manpagez.com 2000-2026
Individual documents may contain additional copyright information.