pete, I've no intention to annoy you, but I'd appreciate it a lot if somebody could help me to find the error. I've updated the code, with your writetofile idea.
#!/usr/bin/env perl
use Exception::Class
( 'MyException' );
sub divbyz {
my $z = 0;
eval { my $result = ( 23 / $z ) }
or MyException->throw( error => 'I feel funny.' );
}
sub writeit {
eval { open my $FH, ">unwritable_file" }
or MyException->throw( error => 'I feel bad.' );
}
or MyException->throw( error => 'I feel bad.' );
# try
eval {
#divbyz();
writeit();
};
# my $e;
# catch
if ( my $e = Exception::Class->caught('MyException') )
{
warn $e->error, "\n", $e->trace->as_string, "\n";
exit;
}
else
{
print "finally \n";
$e = Exception::Class->caught();
ref $e ? $e->rethrow : die $e;
}
So if I run this I come to the conclusion, that there is nothing wrong with the handling code.
Dependent on which of this two subroutines you run, the flow takes an other conditional.
divbyz();
writeit();
divbyz(): Goes into the first if clause (catch)
writeit(): Goes into the last else clause (finally).
So how can I catch the exception caused by writeit()?
ben
| [reply] [d/l] [select] |
First of all, the file open does not need to go into an eval. Secondly, as far as I can see the else will trigger on a successful run (ie no error)
Are you sure the uunwritable file is really unwritable? and that you arent just successfully opening the file?
UPDATE: Looking at the examples in the docs for Exception::Class they seem to make the assumption that your eval always throws an exception. Try changing
ref $e ? $e->rethrow : die $e;
to
ref $e ? $e->rethrow : die $e if $e;
It says that caught without args simply returns $@ (which if successful will be undef), so that will keep it from throwing the empty die you are seeing
-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
| [reply] [d/l] [select] |
> Are you sure the uunwritable file is really unwritable?
OK, I think that's the problem. I made the file unread/unwritable (chmod 111) but this doesn't seems to make the file unwritable. Hmm... How can I raise this exception?
| [reply] |