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

Dear Master Monks,

I would like some advice on creating a MyModule::Base class and MyModule::Exception class.

I know of Exception:Class, but just want some Best Practices ( I have the Perl Best Practices book, so I'll be reading up on the Errors chapter too) on how to go about it.

Basically I want to put a constructor in my Base class and a debug and error method in the Base class which use the MyModule::Exception class, to be used by the rest of my modules in the distribution.

Any good module or nodes to reference?

All advice greatly appreciated.

Thanks,
Gavin.

Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!

Replies are listed 'Best First'.
Re: Advice on a Base and Exception Class
by perrin (Chancellor) on Jan 18, 2006 at 19:47 UTC
    My advice for use of Exception::Class is to only make exception classes for errors that you can catch, correct and continue. If all you plan to do with the exception is log it and stop, just use croak. Most errors don't require exception classes.

      That was my plan, so looks like I should just continue to use croak.

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!
Re: Advice on a Base and Exception Class
by dragonchild (Archbishop) on Jan 18, 2006 at 19:17 UTC
    Do you need an exception class? Are you planning on throwing exceptions everywhere and wrapping everything in an eval-block? I, personally, find exceptions rather annoying. If I have an abnormal termination, croak is generally good enough. If I don't have an abnormal termination, then I should just be using return values.

    As for a base class, why not just use one of the gazillion options on CPAN? Pick one and learn to like it. For the most part, they're all pretty similar.


    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

      Update: What about Class::Base or can/should I just use any other distros Base class?

      Good point. I'll browse CPAN for a Base class.

      I wasn't planning on throwing everywhere, so it maybe makes sense to continue to use croak. I just wanted to know the convention.

      Walking the road to enlightenment... I found a penguin and a camel on the way.....
      Fancy a yourname@perl.me.uk? Just ask!!!