Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by ggvaidya (Pilgrim)
on Sep 16, 2008 at 04:47 UTC ( #711608=note: print w/replies, xml ) Need Help??


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

I completely agree, eval-die is flexible enough for most use. Is there any advantage with hierarchical exceptions which is worth the addition code and modules?

The original problem can be written as:

use warnings; use strict; use 5.0100; eval { print 10/0; # equivalent to: die "Illegal division by zero"; }; if($@) { if($@ =~ /Illegal division by zero/) { print "I feel funny\n"; exit; } else { die $@; } } # No exception thrown. print "What do you know, Perl can divide by zero.\n";

Updated: this code doesn't work properly before Perl 5.10, so I added a 'use' statement to stop it.

Replies are listed 'Best First'.
Re^3: Catching a 'division by zero' error with Exception::Class
by mr_mischief (Monsignor) on Sep 19, 2008 at 23:26 UTC
    I hope it's helpful to someone to point out that your solution works in 5.10.0 but in 5.8.8 perl dies with the familiar "Illegal division by zero at foo line 5." error.

    In fact, pasting that into perl 5.8.8's standard input instead of putting it in a file exits the shell in which it was run. That may not be the sort of thing people appreciate from example code.

      Thanks for pointing that out! I've put a "use 5.10" line in so it won't run unchanged on pre-5.10 perls. Do you think that's good enough?
        It still exits the shell under which perl is running if it's pasted into 5.8.8's standard input.

        This version still issues commands to the shell but one of them isn't an exit:

        use warnings; use strict; use 5.0100; eval { print 10/0; # equivalent to: die "Illegal division by zero"; }; if($@) { if($@ =~ /Illegal division by zero/) { print "I feel funny\n"; exit; } else { die $@; } } # No exception thrown. print "What do you know, Perl can divide by zero.\n";

        When the code is placed in a file, a proper insufficient version abend happens.

        I'm not sure why exactly this is, but it is obviously some sort of issue with use version; not being processed as one might expect when the code source is standard input. I'm not sure in general how important that is to have fixed, since it is kind of a degenerate case. It'd be useful for example snippets, though, if pasting into the standard input stream worked the same way as writing to a file and calling perl with that file as an argument.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2019-06-17 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Is there a future for codeless software?



    Results (80 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!