jbenezech has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,
I'm investigating this Error.pm module and can't seem to make it work as advertised. I'm running ActivePerl 5.8.8 on XP.
I created the following custom Exception:
package DB::DBException; use base qw(Error); sub new { my ($self, $msg) = @_; my @args = (); local $Error::Depth = $Error::Depth + 1; local $Error::Debug = 1; # Enables storing of stacktrace $self->SUPER::new(-text => $msg, @args); return $self; }

and a test.pl:
use strict; use warnings; use Error qw(:try); use DB::DBException; try { throw DB::DBException("Connect exception"); print "OK\n"; } catch DB::DBException with { print "DBException"; } catch Error with { print "Error"; }; print "finished";


My problem is that the DBException is never caught. This test alwasy ends up on the catch Error block. If I remove the catch Error block, the program then dies at the time the exception is thrown: DB::DBException at C:/Perl/site/lib/Error.pm line 184.
I think I pretty much followed the module example so what I am doing wrong there ?
Thanks for a little word of wisdom

Replies are listed 'Best First'.
Re: Error.pm and custom Exception
by ELISHEVA (Prior) on Feb 24, 2009 at 11:11 UTC

    Briefly, new(...) is a class method whose first parameter is the name of the class. So, what you are calling $self is actually a string rather than an error object. And when you return $self, you are returning the class name string from your constructor rather than the error object.

    Your class definition should look more like this: (untested code)

    use strict; use warnings; package DB::DBException; use base qw(Error); sub new { my ($sClass, $msg) = @_; my @args = (); local $Error::Depth = $Error::Depth + 1; local $Error::Debug = 1; # Enables storing of stacktrace # *** this is the key line change *** my $self = $sClass->SUPER::new(-text => $msg, @args); return $self; }
Re: Error.pm and custom Exception
by fmerges (Chaplain) on Feb 24, 2009 at 11:57 UTC

    Hi,

    As said already, you don't get $self in the constructor... it's $class, so you should have:

    my $self = $class->SUPER::new(...)

    Beside of this, I would strongly recommend you to not use Error.pm, based on my own experience. Some reasons:

    * If you forget the ; at the end of your try...catch block you will have hard time trying to find obscure bugs, what you see in the code is not what is being executed.
    * You have to include in every place you want to use the try...catch syntax sugar the 'use Error qw(:try)' and not only the subclassed exeption class. You will not get compiling error, it will just not work as expected.
    * Debugging is also painful, the code executed inside of the try...catch blocks are wrapped, so it makes debugging using the debugger a pain; check it out for yourself.

    For these reasons I would rather recommend you to use Exception::Class instead.

    Regards,

    fmerges at irc.freenode.net
Re: Error.pm and custom Exception
by Anonymous Monk on Feb 24, 2009 at 12:47 UTC
    You will want to avoid the DB namespace.

    DB - programmatic interface to the Perl debugging API

      Cheers to all for the spot on answer and extra tips