From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,7675210751e9faaa X-Google-Attributes: gid103376,public From: "Matthew Heaney" Subject: Re: Handling exceptions to create an atomic operation Date: 1999/12/13 Message-ID: <38552a40_2@news1.prserv.net>#1/1 X-Deja-AN: 560146964 Content-transfer-encoding: 7bit References: <87r9h2ecol.fsf@deneb.cygnus.argh.org> Content-Type: text/plain; charset="US-ASCII" X-Complaints-To: abuse@prserv.net X-Trace: 13 Dec 1999 17:17:52 GMT, 32.101.8.153 Organization: Global Network Services - Remote Access Mail & News Services Mime-version: 1.0 Newsgroups: comp.lang.ada Date: 1999-12-13T00:00:00+00:00 List-Id: In article <87r9h2ecol.fsf@deneb.cygnus.argh.org> , " "@deneb.cygnus.argh.org (Florian Weimer) wrote: > An operation which internally creates several objects should appear to > the caller as one operation which can either complete fail or succeed. > > The following code (using the POSIX bindings) illustrates the problem: [snipped] > If one of the initialization steps fails, the steps executed so far > have to be reverted. A few possible solution came to my mind: > > - Nesting exception handler blocks (looks horrible because you > quickly get a very deep nesting level) The blocks only require partial nesting: begin begin Open_Template (Proc_Template); exception when others => return; -- or whatever end; begin Create_Pipe (Read_End => Input_Read, Write_End => Input_Write); exception return; end; end; > - Use local variables to keep track of the operations which have > already been completed, and one big exception handler which makes > the necessary cleanups (I don't know yet whether I like this one > or not) You could combine these ideas; for example, the outermost exception handler checks to see if the proc_template is open (assuming there's a predicate function to do that), and then closes it if it is. > - (The C style: goto in exception handler to cleanup code -- > fortunately, this one doesn't work...) If you have separate blocks for each statement, you can jump from an exception handler to a location between blocks, eg ... begin Create_Pipe (Read_End => Error_Read, Write_End => Error_Write); exception when others => goto Do_Close_Template; end; ... <> begin Close_Template (Proc_Template); exception when others => end; -- Creationists attempt to draw a line between evolutionary biology and the rest of science by remarking that large-scale evolution cannot be observed. This tactic fails. Large-scale evolution is no more inaccessible to observation than nuclear reactions or the molecular composition of water. Abusing Science: The Case Against Creationism Philip Kitcher