Reputation: 63
How to catch exception from inner eval {}?
#!/usr/bin/perl
use strict;
use warnings;
use Try::Tiny;
use Exception::Class ('T');
use Data::Dumper;
try {
eval {
T->throw("Oops");
};
} catch {
print Dumper \$_;
};
}
We have got not Exception::Class submodule, but scalar instead. More precisely, I have a lot of legacy code with require, and require seems to use eval inside.
Upvotes: 3
Views: 1517
Reputation: 385655
You can automatically upconvert exceptions as follows:
#!/usr/bin/perl
use strict;
use warnings;
use feature qw( say );
use Try::Tiny;
use Exception::Class ('T');
$SIG{__DIE__} = sub {
die($_[0]) if !defined($^S);
die($_[0]) if ref($_[0]) eq 'T';
T->throw($_[0]);
};
try {
die "foo";
} catch {
say ref($_) || "Not reference";
print $_;
};
try {
T->throw("foo");
} catch {
say ref($_) || "Not reference";
say $_;
};
Upvotes: 3
Reputation: 118605
If an exception is encountered inside an eval
block, the return value of the block is undef
(or an empty list in list context) and Perl sets the special variable $@
with the error message. The error message is usually a simple scalar but it can be a reference or blessed reference -- one way that it gets set is with the argument to a die
call, and any type of value may be passed to that function.
try {
eval {
T->throw("Oops"); 1;
} or do {
warn "Exception caught in eval: $@";
# rethrow the exception outside eval
if (ref($@) eq 'T') {
$@->rethrow;
} else {
T->throw("Oops: $@");
}
}
} catch {
print Dumper \$_;
};
Upvotes: 1