Node:Handling Errors, Previous:Dynamic Wind, Up:Control Mechanisms



26.10 How to Handle Errors in C Code

Error handling is based on catch and throw. Errors are always thrown with a key and four arguments:

In addition to catch and throw, the following Scheme facilities are available:

scm-error key subr message args rest Scheme Procedure
Throw an error, with arguments as described above.

error msg arg ... Scheme Procedure
Throw an error using the key 'misc-error. The error message is created by displaying msg and writing the args.

display-error stack port subr message args rest Scheme Procedure
scm_display_error (stack, port, subr, message, args, rest) C Function
Display an error message to the output port port. stack is the saved stack for the error, subr is the name of the procedure in which the error occurred and message is the actual error message, which may contain formatting instructions. These will format the arguments in the list args accordingly. rest is currently ignored.

The following are the error keys defined by libguile and the situations in which they are used:

26.10.1 C Support

SCM scm_error (SCM key, char *subr, char *message, SCM args, SCM rest)

Throws an error, after converting the char * arguments to Scheme strings. subr is the Scheme name of the procedure, NULL is converted to #f. Likewise a NULL message is converted to #f.

The following procedures invoke scm_error with various error keys and arguments. The first three call scm_error with the system-error key and automatically supply errno in the "rest" argument: scm_syserror generates messages using strerror, scm_sysmissing is used when facilities are not available. Care should be taken that the errno value is not reset (e.g. due to an interrupt).

Exception handlers can also be installed from C, using scm_internal_catch, scm_lazy_catch, or scm_stack_catch from libguile/throw.c. These have not yet been documented, however the source contains some useful comments.