Eiffel exception not work

242 views Asked by At

I'm trying to use an exception like in the class below, but the program always fails when I call the kivetel method. I'd think that it'll just call retry part, than it will satisfy the postcondition. But it fails with "y_above_zero" postcond violation.

class
KEYWORDS
create
    make
feature
    y:INTEGER

make
do
end

kivetel
do
ensure
    y__above_zero: y > 0
rescue
    y := 20
    retry
end
end
2

There are 2 answers

2
Jocelyn On BEST ANSWER

This is the expected behavior when you run it under EiffelStudio, i.e under the debugger. If you run it outside ... from the console for instance, you won't notice anything, the execution will go through the rescue clause and retry and continue as expected.

But under debugger, anytime there is an assertion violation, or an exception, the debugger will catch it and popup the dialog.

(note this is possible to ignore specific kind of exception, if this is really bothering you).

0
U. Windl On

The example shown is not representative for exception handling because of the following reasons:

  1. There is no exception raised in the do...end block
  2. Routine kivetel does nothing, so there cannot be an exception raised
  3. The routine kivetel is not correct (it does not fulfill the postcondition).

So basically your rescue block is not called, because the exception triggered (postcondition violated) is not raised inside the routine.