develooper Front page | perl.perl5.porters | Postings from January 2021

Re: Adding a try/catch syntax to Perl

Thread Previous | Thread Next
From:
=?UTF-8?Q?Branislav_Zahradn=C3=ADk?=
Date:
January 27, 2021 11:55
Subject:
Re: Adding a try/catch syntax to Perl
Message ID:
CAB=rbOmC8xiak1dyuTYN=FNK6mKUM54j0mRz6Myqc1KBjmPdfg@mail.gmail.com
I will repeat my objections against try/catch syntax from code maintenance
point of view (assuming that maintenance is 80% of code base life-cycle)

try / catch is type of construct where usually huge chunk of code comes
before smaller one.
It's common rule to put smaller parts ahead of larger parts in if
statement, so why not to do same with exception handing?

You are working on FINALLY block. So implement CATCH (condition) BLOCK same
way (valid since declaration).
At the end result code will be:
- declare how to handle unexpected behaviour (aka: install handler)
- do something

Behaviour will be consistent with signal handling, even you can extend this
syntax into CATCH (SIGNAL::FOO)

It will be also nice to have conditional exception handling (including
FINALLY):
    CATCH (connection error) { sleep 10; redo; } if $retry_allowed;

and last but not least: you can easily implement less expressive variant
(try/catch/finally) using FINALLY / CATCH blocks

Example (pseudo code):
sub q {
   CATCH (DBI::Connection::Error) { ... }
   my $dbh = connect;
   FINALLY { $dbh->disconnect }

   CATCH (no rows selected exception) { return; }
   CATCH (sql exception) { $log->log; redo BLOCK; }

   BLOCK:
     $dbh->do (...);
}

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