Node:Handling Errors, Previous:Dynamic Wind, Up:Control Mechanisms
Error handling is based on
throw. Errors are
always thrown with a key and four arguments:
~Scan be embedded within the message: they will be replaced with members of the args list when the message is printed.
~Aindicates an argument printed using
~Sindicates an argument printed using
write. message can also be
#f, to allow it to be derived from the key by the error handler (may be useful if the key is to be thrown from both C and Scheme).
~Stokens in message. Can also be
#fif no arguments are required.
'system-error, this contains the C errno value. Can also be
#fif no additional objects are required.
In addition to
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 |
|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:
error-signal: thrown after receiving an unhandled fatal signal such as SIGSEGV, SIGBUS, SIGFPE etc. The rest argument in the throw contains the coded signal number (at present this is not the same as the usual Unix signal number).
system-error: thrown after the operating system indicates an error condition. The rest argument in the throw contains the errno value.
numerical-overflow: numerical overflow.
out-of-range: the arguments to a procedure do not fall within the accepted domain.
wrong-type-arg: an argument to a procedure has the wrong type.
wrong-number-of-args: a procedure was called with the wrong number of arguments.
memory-allocation-error: memory allocation error.
stack-overflow: stack overflow error.
regex-error: errors generated by the regular expression library.
misc-error: other errors.
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.