| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (11 out of 13)

Signaling Errors in a Procedure

You can use the signal statement to signal an error during procedure execution. Such an error is essentially the same as an error signaled by G2 itself. The only difference is that the signal statement, rather than G2, supplies the error object.

The syntax of the signal statement is:

When a signal statement executes, G2 looks for a block error handler whose class matches that of the error-object specified in the statement. If G2 finds such a handler, it invokes the handler, passing it error-object. If G2 does not find a block error handler, it invokes the default error handler on error-object.

Thus the signal statement, rather than G2, defines the type of a signalled error. This feature, in conjunction with the ability to define error classes using multiple inheritance, allows very complex handling of signaled errors.

The examples in this section assume that you have read Handling Errors in a Procedure, and do not reiterate the detailed descriptions of error handling that appear in that section.

Signaling the Default Error Handler

The following procedures are demonstrate-default-error-handler, the same procedure that appeared in Default Handler Example, and sigproc, the procedure that was undefined in that example. The procedure sigproc contains an example of a signal statement:



The signal statement in the example specifies an error object of class zerodivide, a subclass of error. Calling demonstrate-default-error-handler calls sigproc with an argument of 0, invoking sigproc's signal statement on an error object of class zerodivide.

Since no block error handler is in effect, G2 passes zdev to the default error handler. The system-defined default error handler posts the following to the Operator Logbook:


The system-defined default error handler has added additional information to the error-description of zdev. G2 provides such information as a convenience whenever a signaled error reaches the default error handler.

Signalling a Block Error Handler

The following procedures are demonstrate-block-error-handler, the same procedure that appeared in Block Error Handler Example, and sigproc, the procedure that was undefined in that example.



Calling demonstrate-default-error-handler calls sigproc with an argument of 0, invoking sigproc's signal statement on zdev. G2 passes zdev to the block error handler of the calling block, which posts the following to the Message Board (not the logbook):


When a signal statement communicates directly with a block error handler, G2 does not add any additional information: it uses the error object exactly as supplied by the signal statement.

| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (11 out of 13)

Copyright © 1997 Gensym Corporation, Inc.