Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Create binding on a tied dbm fileTest a filehandle for its end (Berikutnya)
Input and output functions

Raise an exception or bail out

Daftar Isi

  • die LIST

    die raises an exception. Inside an eval the error message is stuffedinto $@ and the eval is terminated with the undefined value.If the exception is outside of all enclosing evals, then the uncaughtexception prints LIST to STDERR and exits with a non-zero value. If youneed to exit the process with a specific exit code, see exit.

    Equivalent examples:

    1. die "Can't cd to spool: $!\n" unless chdir '/usr/spool/news';
    2. chdir '/usr/spool/news' or die "Can't cd to spool: $!\n"

    If the last element of LIST does not end in a newline, the currentscript line number and input line number (if any) are also printed,and a newline is supplied. Note that the "input line number" (alsoknown as "chunk") is subject to whatever notion of "line" happens tobe currently in effect, and is also available as the special variable$.. See $/ in perlvar and $. in perlvar.

    Hint: sometimes appending ", stopped" to your message will cause itto make better sense when the string "at foo line 123" is appended.Suppose you are running script "canasta".

    1. die "/etc/games is no good";
    2. die "/etc/games is no good, stopped";

    produce, respectively

    1. /etc/games is no good at canasta line 123.
    2. /etc/games is no good, stopped at canasta line 123.

    If the output is empty and $@ already contains a value (typically from aprevious eval) that value is reused after appending "\t...propagated".This is useful for propagating exceptions:

    1. eval { ... };
    2. die unless $@ =~ /Expected exception/;

    If the output is empty and $@ contains an object reference that has aPROPAGATE method, that method will be called with additional fileand line number parameters. The return value replaces the value in$@; i.e., as if $@ = eval { $@->PROPAGATE(__FILE__, __LINE__) };were called.

    If $@ is empty then the string "Died" is used.

    If an uncaught exception results in interpreter exit, the exit code isdetermined from the values of $! and $? with this pseudocode:

    1. exit $! if $!; # errno
    2. exit $? >> 8 if $? >> 8; # child exit status
    3. exit 255; # last resort

    The intent is to squeeze as much possible information about the likely causeinto the limited space of the system exitcode. However, as $! is the valueof C's errno, which can be set by any system call, this means that the valueof the exit code used by die can be non-predictable, so should not be reliedupon, other than to be non-zero.

    You can also call die with a reference argument, and if this is trappedwithin an eval, $@ contains that reference. This permits moreelaborate exception handling using objects that maintain arbitrary stateabout the exception. Such a scheme is sometimes preferable to matchingparticular string values of $@ with regular expressions. Because $@ is a global variable and eval may be used within object implementations,be careful that analyzing the error object doesn't replace the reference inthe global variable. It's easiest to make a local copy of the referencebefore any manipulations. Here's an example:

    1. use Scalar::Util "blessed";
    2. eval { ... ; die Some::Module::Exception->new( FOO => "bar" ) };
    3. if (my $ev_err = $@) {
    4. if (blessed($ev_err) && $ev_err->isa("Some::Module::Exception")) {
    5. # handle Some::Module::Exception
    6. }
    7. else {
    8. # handle all other possible exceptions
    9. }
    10. }

    Because Perl stringifies uncaught exception messages before display,you'll probably want to overload stringification operations onexception objects. See overload for details about that.

    You can arrange for a callback to be run just before the diedoes its deed, by setting the $SIG{__DIE__} hook. The associatedhandler is called with the error text and can change the errormessage, if it sees fit, by calling die again. See%SIG in perlvar for details on setting %SIG entries, andeval BLOCK for some examples. Although this feature was to be run only right before your program was to exit, this is notcurrently so: the $SIG{__DIE__} hook is currently calledeven inside eval()ed blocks/strings! If one wants the hook to donothing in such situations, put

    1. die @_ if $^S;

    as the first line of the handler (see $^S in perlvar). Becausethis promotes strange action at a distance, this counterintuitivebehavior may be fixed in a future release.

    See also exit(), warn(), and the Carp module.

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Create binding on a tied dbm fileTest a filehandle for its end (Berikutnya)