comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <matthew_heaney@acm.org>
Subject: Re: Handling exceptions to create an atomic operation
Date: 1999/12/13
Date: 1999-12-13T00:00:00+00:00	[thread overview]
Message-ID: <38552a40_2@news1.prserv.net> (raw)
In-Reply-To: 87r9h2ecol.fsf@deneb.cygnus.argh.org

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 =>
           <handle open_template error>
           return;  -- or whatever
      end;

      begin
       Create_Pipe (Read_End => Input_Read, Write_End => Input_Write);
      exception
        <close template>
        return;
      end;

      <individual blocks for each statement>

    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 =>
           <handle create_pipe error only>
           goto Do_Close_Template;
      end;

      ...

      <<Do_Close_Template>> begin
       Close_Template (Proc_Template);
      exception
        when others =>
          <handle close error>
      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




      parent reply	other threads:[~1999-12-13  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-12-04  0:00 Handling exceptions to create an atomic operation Florian Weimer
1999-12-05  0:00 ` Robert A Duff
1999-12-12  0:00   ` Florian Weimer
1999-12-13  0:00 ` Matthew Heaney [this message]
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox