develooper Front page | perl.perl5.porters | Postings from July 2020

Re: try/catch and CLEANUP/FINALLY/&c.

Thread Previous | Thread Next
From:
Felipe Gasper
Date:
July 17, 2020 14:30
Subject:
Re: try/catch and CLEANUP/FINALLY/&c.
Message ID:
E3966648-2BBE-492E-B9E8-5837E23DEEEB@felipegasper.com


> On Jul 17, 2020, at 10:22 AM, Paul LeoNerd Evans <leonerd@leonerd.org.uk> wrote:
> 
> On Fri, 17 Jul 2020 10:18:06 -0400
> Felipe Gasper <felipe@felipegasper.com> wrote:
> 
>> Postscript to this: In my JS work I typically do things like:
>> 
>> =====
>> show_notification('Frobnicating …');
>> 
>> try {
>>    attempt_frobnication();
>>    report_success();
>> }
>> catch (e) {
>>    report_failed_frobnication(e);
>> }
>> finally {
>>    hide_notification();
>> }
>> =====
>> 
>> … in which context I definitely want the “cleanup” logic to fire
>> immediately after the try/catch.
>> 
>> Without a “standard” finally block I could do it with a scope-guard
>> by undef()ing the reference after the catch block. With the proposed
>> CLEANUP {} I’d need an extra code block around the try/catch.
>> 
>> -FG
> 
> Would you not write it as
> 
>  show_notification('Frobnicating ...');
>  CLEANUP { hide_notification() }
> 
>  try {
>    attempt_frobnification();
>    report_success();
>  }
>  catch ($e) {
>    report_failed_frobnification($e);
>  }

Possibly, but then if I or someone else adds logic after the catch block, the CLEANUP won’t fire until after that. Consider:

=====
show_notification('Frobnicating ...');
CLEANUP { hide_notification() }

try {
  attempt_frobnification();
  report_success();
}
catch ($e) {
  report_failed_frobnification($e);
}

blocks_for_10_seconds();

# .. and whatever else
=====

Now the CLEANUP won’t happen until 10 seconds after the try/catch. blocks_for_10_seconds() may also create its own notifications, so there will be ordering problems.

It’s not quite an “action-at-a-distance” problem, but it definitely seems prone to maintenance issues. Basically, the cleanup/finally logic “should” fire as soon as possible; using CLEANUP {} to implement finally will necessitate extra work on the developer’s part to make that happen.

-F
Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About