我正在尝试使用下面类中的异常,但是当我调用该kivetel
方法时程序总是失败。我认为它只会调用重试部分,而不是满足后置条件。但它因违反“y_above_zero”而失败。
class
KEYWORDS
create
make
feature
y:INTEGER
make
do
end
kivetel
do
ensure
y__above_zero: y > 0
rescue
y := 20
retry
end
end
我正在尝试使用下面类中的异常,但是当我调用该kivetel
方法时程序总是失败。我认为它只会调用重试部分,而不是满足后置条件。但它因违反“y_above_zero”而失败。
class
KEYWORDS
create
make
feature
y:INTEGER
make
do
end
kivetel
do
ensure
y__above_zero: y > 0
rescue
y := 20
retry
end
end
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).
由于以下原因,所示示例不能代表异常处理:
do
end
kivetel
什么都不做,所以不会引发异常kivetel
不正确(它不满足后置条件)。所以基本上你的rescue
块不会被调用,因为触发的异常(违反后条件)不会在例程中引发。