Previous: Throw, Up: Exceptions


5.11.7.6 How Guile Implements Exceptions

It is traditional in Scheme to implement exception systems using call-with-current-continuation. Continuations (see Continuations) are such a powerful concept that any other control mechanism — including catch and throw — can be implemented in terms of them.

Guile does not implement catch and throw like this, though. Why not? Because Guile is specifically designed to be easy to integrate with applications written in C. In a mixed Scheme/C environment, the concept of continuation must logically include “what happens next” in the C parts of the application as well as the Scheme parts, and it turns out that the only reasonable way of implementing continuations like this is to save and restore the complete C stack.

So Guile's implementation of call-with-current-continuation is a stack copying one. This allows it to interact well with ordinary C code, but means that creating and calling a continuation is slowed down by the time that it takes to copy the C stack.

The more targeted mechanism provided by catch and throw does not need to save and restore the C stack because the throw always jumps to a location higher up the stack of the code that executes the throw. Therefore Guile implements the catch and throw primitives independently of call-with-current-continuation, in a way that takes advantage of this upwards only nature of exceptions.