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

Re: Try/Catch Exception Objects: Possible?

Thread Previous | Thread Next
From:
Kent Fredric
Date:
July 25, 2013 11:54
Subject:
Re: Try/Catch Exception Objects: Possible?
Message ID:
CAATnKFCni4y2=EN+or=6XH5fdcCNdCZYGLviBMzC-P06fuXQ4A@mail.gmail.com
On 25 July 2013 22:49, David E. Wheeler <david@justatheory.com> wrote:
> On Jul 24, 2013, at 12:49 AM, chromatic <chromatic@wgz.org> 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';
}

foo();

---

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

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

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

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

$perl -MDevel::SimpleTrace  /tmp/e.pl native
#bad
#	at main::foo(/tmp/e.pl:21)
#	at main::(/tmp/e.pl:24)
$perl -MDevel::SimpleTrace  /tmp/e.pl class
#bad

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

-- 
Kent

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