Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Croak or Die?

by Anonymous Monk
on May 08, 2008 at 12:36 UTC ( #685452=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Would someone care to explain the differences between Croak and Die? What is the preffered method?

Comment on Croak or Die?
Re: Croak or Die?
by jettero (Monsignor) on May 08, 2008 at 12:44 UTC
    You use die when the error is something you or your code didn't do right. You use croak when it's something your caller isn't doing right. die "error: $!" indicates the error is on the line where the error occured. croak "error: $!" indicates the error is on the line where the caller called your code.

    -Paul

      Very well put. Thank you.

      That's right, moreover it provides sub routine name from where croak called. This is better way to catch error.

Re: Croak or Die?
by jasonk (Parson) on May 08, 2008 at 12:49 UTC

    When you call die and give it a message that doesn't end with a newline, perl helpfully tacks on the file and line where you called die. Carp provides the 'croak' function, which does essentially the same thing, except instead of telling you the file and line where you called croak, it tells you the file and line where the function containing the croak was called.

    #!/usr/bin/perl -w use strict; use warnings; use Carp qw( croak ); do_something(); sub do_something { croak "croaked!"; } __END__ croaked! at test10.pl line 8 main::do_something() called at test10.pl line 5
    #!/usr/bin/perl -w use strict; use warnings; use Carp qw( croak ); do_something(); sub do_something { die "died!"; } __END__ died! at test10.pl line 8.

    The preferred one is whichever gives you the effect you desire. When writing modules I generally to use die for fatal errors that indicate something is wrong with the method that I'm calling die from, so that the error message points at that method, but use croak for fatal errors that indicate that the method was used incorrectly, so that the error message points towards the caller of the method, rather than the method itself.


    www.jasonkohles.com
    We're not surrounded, we're in a target-rich environment!
Re: Croak or Die?
by Your Mother (Canon) on May 08, 2008 at 15:18 UTC

    Given the monastic nature of the place you might also look into confess (another function from Carp). When dealing with deep code -- in legacy territory where one module calls into a chain of several others which in turn each have some half-baked pointless inheritance scheme -- it can be a sanity saver.

Re: Croak or Die?
by sundialsvc4 (Abbot) on May 08, 2008 at 16:00 UTC

    Amen!

    Frankly, most bugs are easy to resolve once you find them. “Aye, there's the rub...” So these routines give you a traceback.

Re: Croak or Die?
by Anonymous Monk on May 09, 2008 at 02:40 UTC
    >perldoc -f croak No documentation for perl function `croak' found >perldoc -f die die LIST Outside an "eval", ... >perldoc Carp ... DESCRIPTION The Carp routines are useful in your own modules because they act +like die() or warn(), but with a message which is more likely to be use +ful to a user of your module. ...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2014-10-20 10:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (75 votes), past polls