http://www.perlmonks.org?node_id=1048828

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

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?

        Regards
        McA

      Thank you for the fast reply.