Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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.
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 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.

    HTH

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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://114917]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2014-09-18 00:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (101 votes), past polls