Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Catching a 'division by zero' error with Exception::Class

by dreadpiratepeter (Priest)
on Sep 15, 2008 at 14:36 UTC ( [id://711472]=note: print w/replies, xml ) Need Help??


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

That won't work. the system dies with a division by 0 error when the 23/0 happens. The or clause that throws your error never occurs.
You would have to either, throw the exception outside of the eval, or override the default die bahavior to throw the exception instead.
Off the top of my head, I am not familiar enough with the exception classes to answer, but I'm sure there are other monks that will.
UPDATE: Actually now that I think about it, your confusion occurs because Exception::Class is more for trapping application errors than for trapping underlying system errors.
A better example for testing would be:
open my $FH ">unwritable_file" or MyException->throw( error => 'I feel + funny.' );


-pete
"Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."

Replies are listed 'Best First'.
Re^2: Catching a 'division by zero' error with Exception::Class
by baurel (Sexton) on Sep 15, 2008 at 16:00 UTC
    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
      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
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://711472]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-19 20:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found