14 Condition Signalling and Handling

Conditions are handled with a function called a handler . Handlers are established dynamically and have dynamic scope and extent. Thus, when a condition is signalled, the processor will call the dynamically closest handler. This can accept, resume or decline the condition (see with-handler for a full discussion and definition of this terminology). If it declines, then the next dynamically closest handler is called, and so on, until a handler accepts or resumes the condition. It is the first handler accepting the condition that is used and this may not necessarily be the most specific. Handlers are established by the special form with-handler.


14.0.16 signal
function


Arguments

condition :

The condition to be signalled.

function :

The function to be called if the condition is handled and resumed, that is to say, the condition is continuable, or () otherwise.

threadopt :

If this argument is not supplied, the condition is signalled on the thread which called signal, otherwise, thread indicates the thread on which condition is to be signalled.

Result

signal should never return. It is an error to call signal’s continuation.

Remarks

Called to indicate that a specified condition has arisen during the execution of a program. If the third argument is not supplied, signal calls the dynamically closest handler with condition and continuation. If the second argument is a subclass of function, it is the resume continuation to be used in the case of a handler deciding to resume from a continuable condition. If the second argument is (), it indicates that the condition was signalled as a non-continuable condition—in this way the handler is informed of the signaler’s intention.

If the third argument is supplied, signal registers the specified condition to be signalled on thread. The condition must be an instance of the condition class <thread-condition>, otherwise an error is signalled (condition class: <wrong-condition-class>) on the thread calling signal. A signal on a determined thread has no effect on either the signalled or signalling thread except in the case of the above error.

See also

thread-reschedule, thread-value, with-handler.


14.0.17 call-next-handler
special operator


14.0.17.1 Syntax

call-next-handler-form:
( call-next-handler )
Remarks

The call-next-handler special form calls the next enclosing handler. It is an error to evaluate this form other than within an established handler function. The call-next-handler special form is normally used when a handler function does not know how to deal with the class of condition. However, it may also be used to combine handler function behaviour in a similar but orthogonal way to call-next-method (assuming a generic handler function).


14.0.18 with-handler
special operator


14.0.18.1 Syntax

with-handler-form:
( with-handler handler-function
form* )
handler-function:
level-0-form
Arguments

handler-function :

The result of evaluating the handler function expression must be either a function or a generic function. This function will be called if a condition is signalled during the dynamic extent of protected-forms and there are no intervening handler functions which accept or resume the condition. A handler function takes two arguments: a condition, and a resume function/continuation. The condition is the condition object that was passed to signal as its first argument. The resume continuation is the continuation (or ()) that was given to signal as its second argument.

forms :

The sequence of forms whose execution is protected by the handler function specified above.

Result

The value of the last form in the sequence of forms.

Remarks

A with-handler form is evaluated in three steps:

  1. The new handler-function is evaluated. This now identifies the nearest enclosing handler and shadows the previous nearest enclosing handler.
  2. The sequence of forms is evaluated in order and the value of the last one is returned as the result of the with-handler expression.
  3. the handler-function is disestablished as the nearest enclosing handler, and the previous handler function is restored as the nearest enclosing.

The above is the normal behaviour of with-handler. The exceptional behaviour of with-handler happens when there is a call to signal during the evaluation of protected-form. signal calls the nearest closing handler-function passing on the first two arguments given to signal. The handler-function is executed in the dynamic extent of the call to signal. However, any signals occurring during the execution of handler-function are dealt withby the nearest enclosing handler outside the extent of the form which established handler-function. It is an error if there is no enclosing handler. In this circumstance the identified error is delivered to the configuration to be dealt with in an implementation-defined way. Errors arising in the dynamic extent of the handler function are signalled in the dynamic extent of the original signal but are handled in the enclosing dynamic extent of the handler.

Examples

There are three ways in which a handler-function can respond: actions:

  1. The error is very serious and the computation must be abandoned; this is likely to be characterised by a non-local exit from the handler function.
  2. The situation can be corrected by the handler, so it does and then returns. Thus the call to signal returns with the result passed back from the handler function.
  3. The handler function does not know how to deal with the class of condition signalled; control is passed explicitly to the next enclosing handler via the call-next-handler special form.

An illustration of the use of all three cases is given here:

Example 1: handler actions
(defgeneric error-handler (condition)
   method: (((c <serious-error>))
            ... abort thread ...)
   method: (((c <fixable-situation>))
            ... apply fix and return ... )
   method: (((c <condition>) (call-next-handler))))

(with-handler error-handler
      ;; the protected expression
      (something-which-might-signal-an-error))

See also

signal.


14.0.19 error
function


Arguments

condition-class :

the class of condition to be signalled.

error-message :

a string containing relevant information.

init-option* :

a sequence of options to be passed to initialize-instance when making the instance of condition.

Result

The result is ().

Remarks

The error function signals a non-continuable error. It calls signal with an instance of a condition of condition-class initialized fromthe error-message, init-options and a resume continuation value of (), signifying that the condition was not signalled continuably.


14.0.20 cerror
function


Arguments

condition-class :

the class of condition to be signalled.

error-message :

a string containing relevant information.

init-option* :

a sequence of options to be passed to initialize-instance when making the instance of condition.

Result

The result is ().

Remarks

The cerror function signals a continuable error. It calls signal with an instance of a condition of condition-class initialized from the error-message, init-options and a resume continuation value which is the continuation of the cerror expression. A non-() resume continuation signifies that the condition has been signalled continuably.