in reply to Re: Catching a 'division by zero' error with Exception::Class
in thread Catching a 'division by zero' error with Exception::Class

pete,
thanks for your answer, but I think your example 'suffers' from the same problem: The exception cannot be catched!
Output:
Died at foo.pl at line 27.
That means, not the specific error is thrown but the last else clause:
else { $e = Exception::Class->caught(); ref $e ? $e->rethrow : die $e; }
Or am I wrong?
ben

Replies are listed 'Best First'.
Re^3: Catching a 'division by zero' error with Exception::Class
by dreadpiratepeter (Priest) on Sep 15, 2008 at 16:21 UTC
    Actually, yes, you are wrong. In this case (the file opening example), the exception will be thrown. My guess is that your handling code is wrong. I have never used Exception::Class so I don't know how it is wrong, but my guess is that the if condition is not triggered.


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
      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
        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."
Re^3: Catching a 'division by zero' error with Exception::Class
by dwindura (Novice) on Sep 16, 2008 at 06:17 UTC
    The division by zero will raised it's own exception. So what you can do is either catching the exception using the Exception::Class:Base :
    #!/usr/bin/env perl use Exception::Class; # try eval { (my $result = (23/0)); }; # catch if ( my $e = Exception::Class->caught() ) { # You can rethrow the exception with your own like MyException her +e die "ERROR: division by zero\n$e\n" }
    or test the value before doing the division manually:
    #!/usr/bin/env perl use strict; use Exception::Class ( 'MyException' ); my $number1 = 12; my $number2 = 0; my $result; # try eval { if ($number2 == 0) { MyException->throw( error => 'I feel funny.' ); } $result = $number1 / $number2; }; # catch if ( my $e = Exception::Class->caught('MyException') ) { warn $e->error, "\n", $e->trace->as_string, "\n"; warn join ' ', $e->euid, $e->egid, $e->uid, $e->gid, $e->pid, $e->time; exit; } else { $e = Exception::Class->caught(); ref $e ? $e->rethrow : die $e; }
    Hope this help.

    DISCLAIMER: I still learning perl also. Anybody please correct me if I am wrong. Thanks