Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

die or croak

by McA (Priest)
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

Replies are listed 'Best First'.
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?


      Thank you for the fast reply.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1048828]
Approved by moritz
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2018-06-22 06:17 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (121 votes). Check out past polls.