From: " "@deneb.cygnus.argh.org (Florian Weimer)
Subject: Handling exceptions to create an atomic operation
Date: 1999/12/04
Date: 1999-12-04T19:19:38+00:00 [thread overview]
Message-ID: <87r9h2ecol.fsf@deneb.cygnus.argh.org> (raw)
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:
begin
Open_Template (Proc_Template);
Create_Pipe (Read_End => Input_Read, Write_End => Input_Write);
Create_Pipe (Read_End => Output_Read, Write_End => Output_Write);
Create_Pipe (Read_End => Error_Read, Write_End => Error_Write);
Set_File_Action_To_Close (Proc_Template, Input_Write);
Set_File_Action_To_Close (Proc_Template, Output_Read);
Set_File_Action_To_Close (Proc_Template, Error_Read);
Start_Process (PID, Path, Proc_Template);
Close_Template (Proc_Template);
exception
when others => ??? -- What to do?
end;
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)
- 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)
- Cleanup code duplication (error prone)
- (The C style: goto in exception handler to cleanup code --
fortunately, this one doesn't work...)
- Nested subprogram for each operation, one subprogram calls the
next one in the sequence of operations (keeps object creation and
corresponding exception handler together, results in moderate but
constant nesting level)
Perhaps there are even better solutions? Which one is the most
practical and most Ada-like in your opinion?
(BTW: Where can I get usage examples for the POSIX binding? I fear
that my code will look very bizarre to the experienced Ada programmer
if I follow my own ideas of doing things...)
next reply other threads:[~1999-12-04 0:00 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-12-04 0:00 Florian Weimer [this message]
1999-12-05 0:00 ` Handling exceptions to create an atomic operation Robert A Duff
1999-12-12 0:00 ` Florian Weimer
1999-12-13 0:00 ` Matthew Heaney
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox