正在加载图片...
$12.3 AN EXCEPTION MECHANISM 419 12.3 AN EXCEPTION MECHANISM From the preceding analysis follows the exception mechanism that fits best with the object-oriented approach and the ideas of Design by Contract. The basic properties will follow from a simple language addition-two keywords to the framework of the preceding chapters.A library class,EXCEPTIONS,will also be available for cases in which you need to fine-tune the mechanism. Rescue and Retry First,it must be possible to specify,in the text of a routine,how to deal with an exception that occurs during one of its calls.We need a new clause for that purpose;the most appropriate keyword is rescue,indicating that the clause describes how to try to recover from an undesirable run-time event.Because the rescue clause describes operations to be executed when the routine's behavior is outside of the standard case described by the precondition (require),body (do)and postcondition (ensure),it will appear,when present,after all these other clauses: routine is require precondition local ..Local entity declarations... do body ensure postcondition rescue rescue clause end The rescue clause is a sequence of instructions.Whenever an exception occurs during the execution of the normal body,this execution will stop and the rescue clause will be executed instead.There is at most one rescue clause in a routine,but it can find out what the exception was(using techniques introduced later),so that you will be able to treat different kinds of exception differently if you wish to. The other new construct is the retry instruction,written just retry.This instruction may only appear in a rescue clause.Its execution consists in re-starting the routine body from the beginning.The initializations are of course not repeated. These constructs are the direct implementation of the Disciplined Exception Handling principle.The retry instruction provides the mechanism for retrying;a rescue clause that does not execute a retry leads to failure.§12.3 AN EXCEPTION MECHANISM 419 12.3 AN EXCEPTION MECHANISM From the preceding analysis follows the exception mechanism that fits best with the object-oriented approach and the ideas of Design by Contract. The basic properties will follow from a simple language addition — two keywords — to the framework of the preceding chapters. A library class, EXCEPTIONS, will also be available for cases in which you need to fine-tune the mechanism. Rescue and Retry First, it must be possible to specify, in the text of a routine, how to deal with an exception that occurs during one of its calls. We need a new clause for that purpose; the most appropriate keyword is rescue, indicating that the clause describes how to try to recover from an undesirable run-time event. Because the rescue clause describes operations to be executed when the routine’s behavior is outside of the standard case described by the precondition (require), body (do) and postcondition (ensure), it will appear, when present, after all these other clauses: routine is require precondition local … Local entity declarations … do body ensure postcondition rescue rescue_clause end The rescue_clause is a sequence of instructions. Whenever an exception occurs during the execution of the normal body, this execution will stop and the rescue_clause will be executed instead. There is at most one rescue clause in a routine, but it can find out what the exception was (using techniques introduced later), so that you will be able to treat different kinds of exception differently if you wish to. The other new construct is the retry instruction, written just retry. This instruction may only appear in a rescue clause. Its execution consists in re-starting the routine body from the beginning. The initializations are of course not repeated. These constructs are the direct implementation of the Disciplined Exception Handling principle. The retry instruction provides the mechanism for retrying; a rescue clause that does not execute a retry leads to failure
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有