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

Re: Adding a try/catch syntax to Perl

Thread Previous | Thread Next
January 27, 2021 11:55
Re: Adding a try/catch syntax to Perl
Message ID:
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
    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; }

     $dbh->do (...);

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About