next up previous contents index
Next: Memory Management Up: Writing Methods Previous: Adding a Method to

Exception Handling

 

It's important for writing a stable program to do an intensive error checking, especially in an interactive object-oriented environment with features like late binding.

GRAPE provides two general error handling macros

ALERT(<expression>, "error message", <statements>)
ASSURE(<expression>, "error message", <statements>)
which continue execution only if the <expression> is TRUE. Otherwise ASSURE prints the "error message" string together with the corresponding file name and line number and the user is prompted for a response, ALERT pops up a requester with this information instead if a manager is present. Finally the <statements> are executed.

Usually you should use ALERT because the error message will be shown in the control window. The error message of ASSURE could be hidden if the shell it is printed to is in background, in this case the program would seem to be frozen. But ALERT shouldn't be used in methods on low level classes like devices or interactive classes.

If an error occurs in a method NULL should be returned. This can be done by substituting

END_METHOD(NULL)
for <statements>. The <statements> are placed in { ;} by the macros. However, if the <statements> contain a comma not surrounded by brackets the C preprocessor thinks to have found too many arguments. It is possible to hide this comma by using another pair of { }.

An example requiring more then one statement: If a method fails after some object obj has been created, then obj can be deleted before NULL is returned:

ASSURE(expr,
       "method failed",
       {
         GRAPE(obj, "delete")();
         END_METHOD(NULL);
       });
or
ASSURE(expr, "method failed",
       GRAPE(obj, "delete")();
       END_METHOD(NULL));
Plenty of examples can be found in the methods presented below.



SFB 256 Universität Bonn and IAM Universität Freiburg

Copyright © by the Sonderforschungsbereich 256 at the Institut für Angewandte Mathematik, Universität Bonn.