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

Try/Catch Exception Objects: Possible?

Thread Next
From:
David E. Wheeler
Date:
July 22, 2013 21:45
Subject:
Try/Catch Exception Objects: Possible?
Message ID:
18999EEE-0481-4908-B7D5-D89FAC229D6D@justatheory.com
Porters,

I would like to put together a proposal for adding a `feature` to support `try {} catch {}` (or `try { CATCH {}}`) in Perl 5. Before I go blabbing my uninformed ideas about how it should work, I would be interested in learning what thoughts others have had. The basics of what what I’m thinking are:

* try/catch functionality enabled by `use feature 'try'` (or `use v5.20` or whatever)
* Having `die` create exception objects
* Those objects would stringify to the current string format
* Having core language exceptions be objects

My uninformed guesses about implementation are:

* the new syntax would be no big deal, thanks to `feature`.
* Changing `die` to create exception objects would be straight-forward
* Changing core errors to category-specific exceptions would mostly be
  busy work, though maybe it can be derived from autodie

My guess is that the hard part would be in ensuring backward-compatibility. Not all existing exception-handling code can handle even an object that stringfies to its original. Even if it did, there are *so* many modules out there now that throw references instead of strings, and they may not work at all.

Would that be the primary concern about adding exception objects for all exceptions?

If so, how about this: Have `die` create an object, but only set it up for use when the new CATCH OP is executed. Or perhaps even leave the value passed to `die` in the $@ global, but set lexical $_ to the new core object. So in all the places exceptions were previously used, they should be the same as ever, and only upgraded to core exception objects in CATCH blocks.

Crazy? Impossible? Terrifying?

Thanks,

David


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