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

Try/Catch Exception Objects: Possible?

Thread Next
David E. Wheeler
July 22, 2013 21:45
Try/Catch Exception Objects: Possible?
Message ID:

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?



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