Dear Perlers,

I am trying to figure out the flow of a try catch block after executing the 
'next' statement. In the try statement after illegal division by zero the 
program flow reaches catch block and then executes the 'next' statement. After 
executing the next statement the control flow has to skip the following 
statement

say "The sum of (@total) is $sum\n";


In otherwords, I want to ignore the say statement in case if an exception is 
raised. (In the output of the given program, it is printing the total)

There was an error
The sum of (1024 2048 512 0) is 3584

Please help.
 
[code]
use Try::Tiny;
use 5.010;

my @nums = qw/1024 2048 512 0 4096 256 128/;
my @total;

foreach my $n (@nums) {
    try {
        push @total, $n;
        $sum += $n;
        my $result = 1024 / $n;
    }
    catch {
        #no warnings;
        next if /Illegal division/;
        say "Hello ...\n";
    }
    finally {
        say @_ ? "There was an error" : "Everything worked";
    };
    say "The sum of (@total) is $sum\n";
}
[/code]

[output]
Exiting subroutine via next at C:/Users/shaji 
kalidasan/workspace/juno-sr1/shaji/Rajesh Perl Project/raid.pl line 15.
Everything worked
The sum of (1024) is 1024

Everything worked
The sum of (1024 2048) is 3072

Everything worked
The sum of (1024 2048 512) is 3584

There was an error
The sum of (1024 2048 512 0) is 3584

Everything worked
The sum of (1024 2048 512 0 4096) is 7680

Everything worked
The sum of (1024 2048 512 0 4096 256) is 7936

Everything worked
The sum of (1024 2048 512 0 4096 256 128) is 8064
[/output]


best,
Shaji 
-------------------------------------------------------------------------------
Your talent is God's gift to you. What you do with it is your gift back to God.
-------------------------------------------------------------------------------

Reply via email to