From: Matt Borchers <mattborchers@gmail.com>
Subject: potential Ada feature - comments?
Date: Tue, 10 Sep 2019 10:52:54 -0700 (PDT)
Date: 2019-09-10T10:52:54-07:00 [thread overview]
Message-ID: <fc3ac55b-d5c8-4255-9889-7f4b3fe428d0@googlegroups.com> (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
next reply other threads:[~2019-09-10 17:52 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-09-10 17:52 Matt Borchers [this message]
2019-09-10 19:13 ` potential Ada feature - comments? 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
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox