Discussion Locked

Perl TechMail

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Handle errors with warn and die:

The Feb. 18, Perl TechMail discusses error handling. Have you used warn and die in your Perl applications? Share your thoughts on error handling methods.

1 reply

by andrew houghton

Often overlooked is the use of die with eval for basic catch/throw operations. A simple example is:
sub _package { return caller; }
my @package = _package();
my $package = join($;,@package[0..($#package-1)]);
sub mySub {
my $v=shift(@_);
if(not defined($v)) {
die("undefinedException$;parameter v not defined$;$package$;");
}
return $v;
}
# You could put everything above this comment in a .pm module, everything below this comment in a .pl file, and it will still work assuming you "use" the .pm module and the .pm module exports its mySub with the Perl Exporter package.
my $v=eval{mySub(undef);};
if($@ ne ''){
my @exception=split($;,$@);
my $type=$exception[0];
my $msg=$exception[1];
my $package=$exception[2];
my $script=$exception[3];
my $line=$exception[4];
$line =~ s/^.*?\ +(\d+)\.$/$1/sx;
print "type: $type\n";
print "message: $msg\n";
print "package: $package\n";
print "script: $script\n";
print "line: $line\n";
}else{
print "value: $v\n";
}

This discussion is closed to new replies.