Previous: Dynamic Wind, Up: Control Mechanisms


5.11.10 How to Handle Errors

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:

— Scheme Procedure: display-error stack port subr message args rest
— C Function: scm_display_error (stack, port, subr, message, args, rest)

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:

5.11.10.1 C Support

In the following C functions, SUBR and MESSAGE parameters can be NULL to give the effect of #f described above.

— C Function: SCM scm_error (SCM key, char *subr, char *message, SCM args, SCM rest)

Throw an error, as per scm-error above.

— C Function: void scm_syserror (char *subr)
— C Function: void scm_syserror_msg (char *subr, char *message, SCM args)

Throw an error with key system-error and supply errno in the rest argument. For scm_syserror the message is generated using strerror.

Care should be taken that any code in between the failing operation and the call to these routines doesn't change errno.

— C Function: void scm_num_overflow (char *subr)
— C Function: void scm_out_of_range (char *subr, SCM bad_value)
— C Function: void scm_wrong_num_args (SCM proc)
— C Function: void scm_wrong_type_arg (char *subr, int argnum, SCM bad_value)
— C Function: void scm_memory_error (char *subr)

Throw an error with the various keys described above.

For scm_wrong_num_args, proc should be a Scheme symbol which is the name of the procedure incorrectly invoked.