Difference between revisions of "Perl eval"
From Teknologisk videncenter
m |
m (→Throwing an exception) |
||
Line 21: | Line 21: | ||
{ | { | ||
# catch | # catch | ||
+ | } | ||
+ | </source> | ||
+ | =Example= | ||
+ | <source lang=perl> | ||
+ | #!/usr/bin/perl | ||
+ | |||
+ | eval | ||
+ | { | ||
+ | my $res = 1/0; | ||
+ | }; | ||
+ | if($@) | ||
+ | { | ||
+ | print("Help - I'm dying\n"); | ||
+ | print(" Perl Script.: $0\n"); | ||
+ | print(" Perl verson.: $]\n"); | ||
+ | print(" Error #.....: ",$@,"\n"); | ||
+ | exit; | ||
} | } | ||
</source> | </source> |
Revision as of 12:39, 27 June 2010
Perl eval can be used as try and catch exception handling.
eval
{
# try
};
if($@)
{
# catch
}
Throwing an exception
eval
{
# try
die "Exception text to be catched";
};
if($@)
{
# catch
}
Example
#!/usr/bin/perl
eval
{
my $res = 1/0;
};
if($@)
{
print("Help - I'm dying\n");
print(" Perl Script.: $0\n");
print(" Perl verson.: $]\n");
print(" Error #.....: ",$@,"\n");
exit;
}