comp.lang.ada
 help / color / mirror / Atom feed
* potential Ada feature - comments?
@ 2019-09-10 17:52 Matt Borchers
  2019-09-10 19:13 ` Dmitry A. Kazakov
  2019-09-11  9:55 ` fabien.chouteau
  0 siblings, 2 replies; 7+ messages in thread
From: Matt Borchers @ 2019-09-10 17:52 UTC (permalink / raw)


I often find myself writing the same code fragment in exception handlers.  That is, I catch and handle the specific exceptions and the cleanup code for the subprogram is identical.  I know that I can (and do) write a cleanup routine to handle this, but even so, the call to the cleanup routine must still be added to every exception case in the handler.

procedure SUBPROGRAM is
   procedure CLEANUP is
   begin
      --free resources
      ...
   end CLEANUP;
begin
   --acquire resources
   ...
exception
   when X =>
      --something for X
      cleanup;
   when Y =>
      --something for Y
      cleanup;
   when Z =>
      --something for Z
      cleanup;
   when others =>
      --general case
      cleanup;
end SUBPROGRAM;

I propose something like the following:

begin
   --acquire resources
   ...
exception
   when X =>
      --something specific for X
      ...
   when Y =>
      --something specific for Y
      ...
   when Z =>
      --something specific for Z
      ...
   when others =>
      --general case
      ...
finally
   --free resources
   ...
end SUBPROGRAM;

The code in the finally block would run immediately before the stack frame is removed for the SUBPROGRAM regardless of whether the exception is re-raised.  I would expect that the 'finally' block would only be allowed immediately after an 'exception' handler block.  The following would NOT be allowed:

begin
...
finally  --syntax error: finally must follow exception handler
  cleanup;
end SUBPROGRAM;

What do you all think?
1) It eliminates redundant code.
2) It eliminates a nested and unnecessary cleanup routine.
3) It would make me feel better when writing exception handlers.

Matt


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2019-09-13  1:14 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-10 17:52 potential Ada feature - comments? Matt Borchers
2019-09-10 19:13 ` Dmitry A. Kazakov
2019-09-11  0:35   ` Matt Borchers
2019-09-11  7:54     ` Dmitry A. Kazakov
2019-09-13  1:14       ` Matt Borchers
2019-09-11 16:00     ` G. B.
2019-09-11  9:55 ` fabien.chouteau

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