comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: potential Ada feature - comments?
Date: Tue, 10 Sep 2019 21:13:13 +0200
Date: 2019-09-10T21:13:13+02:00	[thread overview]
Message-ID: <ql8sk7$6de$1@gioia.aioe.org> (raw)
In-Reply-To: fc3ac55b-d5c8-4255-9889-7f4b3fe428d0@googlegroups.com

On 2019-09-10 19:52, Matt Borchers wrote:
> 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 would rather:

    procedure SUBPROGRAM is
    begin
         --acquire resources
       begin
         ...
         -- keep resources if all OK????
       exception
          when X =>
             --something for X
             raise;
          when Y =>
             --something for Y
             raise;
          when Z =>
             --something for Z
             raise;
          when others =>
             --general case
             raise;
       end;
    exception
       when others =>
          --free resources
    end SUBPROGRAM;

But my preferred method is to use a controlled object with freeing the 
resource inside finalization.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  reply	other threads:[~2019-09-10 19:13 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-10 17:52 potential Ada feature - comments? Matt Borchers
2019-09-10 19:13 ` Dmitry A. Kazakov [this message]
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
replies disabled

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