Search code examples
exceptionerror-handlingeiffel

Error handling in Eiffel and Rescue clause


Is writing one rescue clause at the end of a program to end the program in Eiffel enough to handle exceptions such as pre, postconditions or invariant violations in any of the routines written in the program? Or should I write rescue clause for every function having pre and postconditions to handle exceptions?

I have read Eiffel documentation on error handling but I couldn't figure out.


Solution

  • The answer depends on your expectations. The method is as follows:

    1. Precondition violations are reported to the caller. They indicate a bug in the caller. If bugs are expected or possible in the callers, you can catch and handle them in the callers.

    2. Postcondition violations are reported to the callee. They indicate bugs in the callee. The callee can catch and handle them if such bugs are expected.

    3. Class invariants are established at object creation. If they are violated by the creation procedure or after the execution of a procedure used in a qualified call, they indicate a bug in the callee and can be handled like postcondition violations. Otherwise, they indicate more complex issue involving object dependencies. It could be handled by the caller, but most likely it would be next to impossible to restore correct object state.

    In all cases, assertion violations mean a program bug and whether and how it should be caught and handled depends on your needs. E.g., it's possible to do the handling somewhere between the problematic code and the root procedure.