Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Errors in Module - What to do with them.

by Anonymous Monk
on Sep 26, 2001 at 23:37 UTC ( #114917=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am writing a module to use here at work and am stuck with what to do with errors. I figure I have 2 ways of dealing with them. 1) Let the module kill the script (die) and report the error or 2) Return the error to the caller and let the caller deal with it. Any suggestions on how to properly deal with errors would be much appreciated. Thanks AM
  • Comment on Errors in Module - What to do with them.

Replies are listed 'Best First'.
Re: Errors in Module - What to do with them.
by VSarkiss (Monsignor) on Sep 26, 2001 at 23:49 UTC

    In general, it's poor form for a module to call die or warn. Take a look at the Carp module (part of the standard distribution) and use either Carp::croak or Carp::carp.


Re: Errors in Module - What to do with them.
by suaveant (Parson) on Sep 26, 2001 at 23:43 UTC
    Really, in the end, that is up to the error. In *most* cases it is probably best to return the error to the user, but there can be cases, where things just go absolutely wrong that a die isn't a bad idea (and can still be trapped by eval). But probably best is to return errors, and possibly have an option the user can set on whether they want errors to be fatal or not.

                    - Ant
                    - Some of my best work - Fish Dinner

Re: Errors in Module - What to do with them.
by runrig (Abbot) on Sep 26, 2001 at 23:45 UTC
    Some would say it depends on how fatal the error should be. If you die, and document that that's what happens, you can let the caller deal with it with an 'eval' if he likes, and the error message will be in $@. If there's an error message to report, I usually prefer that to returning undef and putting the error message in some package variable (at least lately).
Re: Errors in Module - What to do with them.
by Anonymous Monk on Sep 27, 2001 at 00:17 UTC
    Thank you for such quick responses. Upon reviewing y'alls hints and the Carp man page I determined that croaking on module init and carping on the other errors made sense. Thanks for such good responses. AM

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://114917]
Approved by root
[vedagiri89]: hi all! i need your help
[vedagiri89]: i'm facing this issue: Attempt to reload aborted.\ nCompilation failed in require at /usr/lib64/perl5/ vendor_perl//APR. pm line 19.\nBEGIN failed--compilatio n aborted at /usr/lib64/perl5/ vendor_perl//APR. pm line 19.
[vedagiri89]: how do fix this issue
[choroba]: This usually happens when you have more than one version of Perl, and you mix the libraries

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2018-06-19 11:15 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (113 votes). Check out past polls.