正在加载图片...
420 WHEN THE CONTRACT IS BROKEN:EXCEPTION HANDLING $12.3 How to fail without really trying The last observation is worth emphasizing: Failure principle Execution of a rescue clause to its end,not leading to a retry instruction, causes the current routine call to fail. So if you have wondered how routines can fail in practice-causing case E4 of the See page 413. exception classification-this is it. As a special case,consider a routine which does not have a rescue clause.In practice this will be the case with the vast majority of routines since the approach to exception handling developed here suggests equipping only a select few routines with such a clause. Ignoring possible local entity declarations,arguments,precondition and postcondition,the routine appears as routine is do body end Then if we consider-as a temporary convention-that the absence of a rescue clause is the same thing as an empty rescue clause,that is to say routine is do body rescue --Nothing here (empty instruction list) end the Failure principle has an immediate consequence:if an exception occurs in a routine without rescue clause it will cause the routine to fail,triggering an exception in its caller. Treating an absent rescue clause as if it were present but empty is a good enough For the exact comven- approximation at this stage of the discussion,but we will need to refine this rule slightly tion see“When there when we start looking at the effect of exceptions on the class invariant. is no rescue clause", page 430. An exception history table If a routine fails,either because it has no rescue clause at all or because its rescue clause executes to the end without a retry,it will interrupt the execution of its caller with a "Routine failed"(E4)exception.The caller is then faced with the same two possibilities: either it has a rescue clause that can execute a successful retry and get rid of the exception,or it will fail too,passing the exception one level up the call chain.420 WHEN THE CONTRACT IS BROKEN: EXCEPTION HANDLING §12.3 How to fail without really trying The last observation is worth emphasizing: So if you have wondered how routines can fail in practice — causing case E4 of the exception classification — this is it. As a special case, consider a routine which does not have a rescue clause. In practice this will be the case with the vast majority of routines since the approach to exception handling developed here suggests equipping only a select few routines with such a clause. Ignoring possible local entity declarations, arguments, precondition and postcondition, the routine appears as routine is do body end Then if we consider — as a temporary convention — that the absence of a rescue clause is the same thing as an empty rescue clause, that is to say routine is do body rescue -- Nothing here (empty instruction list) end the Failure principle has an immediate consequence: if an exception occurs in a routine without rescue clause it will cause the routine to fail, triggering an exception in its caller. Treating an absent rescue clause as if it were present but empty is a good enough approximation at this stage of the discussion; but we will need to refine this rule slightly when we start looking at the effect of exceptions on the class invariant. An exception history table If a routine fails, either because it has no rescue clause at all or because its rescue clause executes to the end without a retry, it will interrupt the execution of its caller with a “Routine failed” (E4) exception. The caller is then faced with the same two possibilities: either it has a rescue clause that can execute a successful retry and get rid of the exception, or it will fail too, passing the exception one level up the call chain. Failure principle Execution of a rescue clause to its end, not leading to a retry instruction, causes the current routine call to fail. See page 413. For the exact conven￾tion see “When there is no rescue clause”, page 430
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有