Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

die

Raises an exception, terminating execution unless caught by an eval block or a try/catch construct.

If called with a string argument, raises that string as the exception message. If the string does not end with a newline, Perl appends at FILE line LINE. If called with a reference (e.g. a hashref or blessed object), the reference is propagated as the exception value in $@.

When called with no arguments (or an empty string / undef), re-raises the current value of $@. If $@ is also empty, dies with "Died".

If there is an active try block (from use feature 'try'), the exception is caught and execution transfers to the catch block instead of propagating.

Synopsis

die "something went wrong";
die "Error at $file line $line\n";
die $exception_object;
die;                              # re-raises $@

Implementation Notes

perl5 (pp_sys.c:583): mark-based list op. If SP - MARK != 1, joins all args with “” via do_join into TARG. Then mess_sv for location if no trailing \n, then die_unwind.

If inside an eval, sets $@, unwinds cxstack to the eval frame, and returns the eval’s retop (so execution continues after the eval). If no eval on the cxstack, prints to stderr and terminates.

See Also

PP runtime: die | warn, eval