Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

die or croak

by McA (Deacon)
on Aug 09, 2013 at 19:21 UTC ( #1048828=perlquestion: print w/ replies, xml ) Need Help??
McA has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

can anybody give me advice on the following issue:

To throw exceptions in modules which will be used by others, should I use die or Carp::croak? And when then why?

I have the feeling that reporting the file and line number of the module (using die) exposes some kind of implementation detail which should be hidden, isn't it?

Best regards
McA

Comment on die or croak
Select or Download Code
Re: die or croak
by moritz (Cardinal) on Aug 09, 2013 at 19:26 UTC

    If you get an exception because you misused the API of a module, you usually want to know where in your the error is, so you're happier when the module called croak().

    Of course that concept breaks down when you write a module that uses another module which uses croak() -- the errors will come from your module, not from your module user's code.

    In the case of internal errors, modules should use die, or maybe even Carp::confess.

    (Oh, and if (as a user) you want to get a backtrace no matter what, use Carp::Always).

      Note that you can exempt your module from croak by setting up @CARP_NOT. That way, your module will be invisible to carp.

        Ahh, that sounds intersting. Can you give some pointers or examples?

        Regards
        McA

      Thank you for the fast reply.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2014-07-30 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (229 votes), past polls