Previous Next Contents

19. Non-local Exits

The return special form mentioned in the section on iteration is an example of a nonlocal return. Another example is the return-from form, which returns a value from the surrounding function:

> (defun foo (x)
    (return-from foo 3)
> (foo 17)

Actually, the return-from form can return from any named block -- it's just that functions are the only blocks which are named by default. You can create a named block with the block special form:

> (block foo
    (return-from foo 7)

The return special form can return from any block named nil. Loops are by default labelled nil, but you can make your own nil-labelled blocks:

> (block nil
    (return 7)

Another form which causes a nonlocal exit is the error form:

> (error "This is an error")
Error: This is an error

The error form applies format to its arguments, then places you in the debugger.

Previous Next Contents