(ref.doc)richieb 270194

Next ark 260194 Prev: bs 240194 Up: Usenet

Newsgroups: comp.lang.c++,comp.std.c++
From: [email protected] (Richard Bielak)
Subject: Re: Exceptions: Are they a Bad Thing?
Date: Thu, 27 Jan 1994 18:23:32 GMT

>> [...me example of Eiffel's preconditions deleted...]
>
>This seems like a distinction without a difference.  In both the C++
>and Eiffel examples the client will get an exception and the logic the
>client requires to recognize and handle this exception will also be
>very similar (though I seem to recall that Eiffel uses a resumptive
>exception handling model, unlike C++).  Am I missing some
>fundamentally different idea here?
>

I think there is a difference which, to me anyway, makes the reason
for exceptions much clearer. The idea is "programming by contract"
(due to Bertrand Meyer) and it says that a caller makes a contract
with the called routine. This contract is expressed in terms of pre-
and postconditions and as with any contract there are obligations and
benefits for both parties:

                   Obligations            Benefits
                ------------------------------------------
         Caller:|  preconditions  |  assume postconditions
                -------------------------------------------
         Callee:|  postconditions |  assume preconditions
                -------------------------------------------

Exceptions are generated when the obligations of the contract are not
met. If preconditions are false the caller gets an exception, if
postconditions are false the callee gets the exception.

Here is an example of a routine that adds and element to an array:

     put (index : INTEGER; new_item : T) is
        require
            (index >= min) and (index <= max)
        do
          ...whatever, but we can assume that the index is within bounds.
        ensure
            item(index) = new_item  -- guarantee that new_item is in the array
        end;


And here is how the out of bounds exceptions could be handled in a
caller:

     stuff_in_array (it : T; a : ARRAY; index : INTEGER) is
        do
           a.put (index, it)

        rescue -- exception handler
           -- If the index is reasonable, resize the array
           -- and retry the routine. If not, fail.
           if a.max + 1 = index then
              a.resize (a.min, a.max+1);
              retry -- re-execute the routine from the begining
           end;
        end;


In Eiffel the pre- and postconditions form part of the routine's
specification and both can be checked at runtime.

Also assertions are inherited and can be modified in the descendants.


automatically generated by info2www version 1.2.2.8