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

Re: Try/Catch Exception Objects: Possible?

Thread Previous | Thread Next
Kent Fredric
July 25, 2013 11:54
Re: Try/Catch Exception Objects: Possible?
Message ID:
On 25 July 2013 22:49, David E. Wheeler <> wrote:
> On Jul 24, 2013, at 12:49 AM, chromatic <> wrote:
>> I can't find the relevant branch at the moment, but I'm not sure there is much
>> left salvaging at this point.
> Bummer, might have been useful.
> I was thinking about what might be the simplest thing to try, and came up with this:
> * Create a dead simple .pm exception class like you describe (Maybe you have that lying around somewhere?)

Of course, "dead simple" and "dead simple and also useful" may be 2
different things.

Take this code, which *seems* simple enough that it might work:

#!/usr/bin/env perl
use strict;
use warnings;
use utf8;

    package ExceptionClass;

    use overload q{""} => sub {
        return $_[0]->{message};
    sub new {
        my ( $self, $message ) = @_;
        return bless { message => $message }, $self;


sub foo {
    die ExceptionClass->new('bad') if $ARGV[0] eq 'class';
    die 'bad' if $ARGV[0] eq 'native';



Doesn't take long before you notice how the string form is superior to
the class form:

$ perl  /tmp/ native
# bad at /tmp/ line 21.
$ perl  /tmp/ class
# bad

And it breaks your standard "get more backtrace" tools:

$ perl -MCarp::Always  /tmp/ native
# bad at /tmp/ line 21.
#	main::foo() called at /tmp/ line 24
$ perl -MCarp::Always  /tmp/ class
# bad

$perl -MDevel::SimpleTrace  /tmp/ native
#	at main::foo(/tmp/
#	at main::(/tmp/
$perl -MDevel::SimpleTrace  /tmp/ class

You have to start doing crazy (and expensive in pureperl) things just
to have a useable exception class if you try using objects.


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