Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

exit or die in error handling?

by Anonymous Monk
on Apr 14, 2005 at 10:09 UTC ( [id://447683]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
I have a doubt, I am posting my code here.
#!/usr/bin/perl $type = $ARGV[0]; $argument = $ARGV[1]; if( $type eq 'job' || $type eq 'member' || $type eq 'file' ) { print "yes..."; } else { print "The parameters for this program are as follows: [job/member/fil +e] [jobnumber/membernumber/filename]"; die; }

or in else condition shall I use
else { print "The parameters for this program are as follows: [job/member/fil +e] [jobnumber/membernumber/filename]"; exit; }

Which one is the best way? (Memory management point of view).
Regards,
John

Edited by Arunbear: Changed title from 'Provide your valuable suggestion', as per Monastery guidelines

Replies are listed 'Best First'.
Re: exit or die in error handling?
by monkey_boy (Priest) on Apr 14, 2005 at 10:16 UTC
    I dont think "Memory management" has anything to do with it.
    They both end the program, however exit() without an argument returns 0 (which is true in the *nix world).

    also, errors should be printed to STDERR not SDTOUT.
    So.. third option..
    else { die("The parameters for this program are as follows: [job/member/fil +e] [jobnumber/membernumber/filename]"); }



    This is not a Signature...
      They both end the program,

      I think the most important difference is that die can be caught by an eval (it is Perl's way of doing exceptions) in other parts of the program, so it does not necessarily end the program. Using die thus makes code more easily reusable, and is a good habit to get into.

Re: exit or die in error handling?
by myuserid7 (Scribe) on Apr 14, 2005 at 10:15 UTC
    Use "exit" if it's not an error. Use "die" if it is. You don't want to worry about memory management for simple things like this - think programmer efficiency!
Re: exit or die in error handling?
by QM (Parson) on Apr 14, 2005 at 15:30 UTC
    I would suggest also looking at one of the Getopt::* modules on how to manage command line parameters (Getopt::Long, or my current favorite, Getopt::Declare). There is also the -s command line switch for parsing command line options.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re: exit or die in error handling?
by Taulmarill (Deacon) on Apr 14, 2005 at 10:17 UTC
    depends on what you want to do. die gives an error message to STDOUT and is used if there is an error. exit on the other hand just ends the script.
    see perldoc -f die and perldoc -f exit
      "die gives an error message to STDOUT"

      No it doesnt! STDERR


      This is not a Signature...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-03-19 07:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found