[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Error with WITH-HANDLER+UNIT?



Quoting "Will Fitzgerald":
> 
> This code fragment catches an error and returns it:
> 
> > (with-handlers
>     ((identity identity))
>      (/ 1 0))
> #<struct:exn:application:divide-by-zero>

Divide-by-zero is a run-time error. Similarly, in plain old Scheme,
detection of an undefined global is performed only when the variable is
referenced at run-time:

 > (with-handlers
     ((identity identity))
     nodefined)
 #<struct:exn:variable>

However, the `unit' form *syntactically* disallows free variables.

> But this doesn't catch the error UNIT throws
> 
> > (with-handlers
>     ((identity identity))
>      (unit (import) (export) some-unknown-variable))
> 
> unit: used an unbound or non-primitive global in: some-unknown-variable

In this case, the exception is raised by the compiler when it sees the
`(with-handlers ...)' expression, not when evaluating the `(unit ...)'
sub-expression or the body of the unit. The exception is raised and
passed to the REPL, which was trying to eval the `(with-handlers ...)'
expression.

Compare to

 > (with-handlers
     ((identity identity))
     (eval '(unit (import) (export) some-unknown-variable)))
 #<struct:exn:syntax>

In this case, the `(with-handlers ...)' expression is succesfully
compiled and then evaluated. In the process of evaluation, the
expression asks that `(unit ...)' is compiled and evaluated. But the
`(unit ...)' experssion is syntactically ill-formed, so an exception is
raised in the call to `eval'.

Matthew