Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: the try/catch example from "Programming Perl" analyzed

by adrianh (Chancellor)
on Aug 19, 2004 at 13:05 UTC ( #384273=note: print w/ replies, xml ) Need Help??


in reply to Re: the try/catch example from "Programming Perl" analyzed
in thread the try/catch example from "Programming Perl" analyzed

In Perl, you also have to handle the condition of a runtime error which is not an object:

You could always encapsulate the extra test in some code:

sub exception (;$) { my $wanted_exception = shift; return unless $@; return 1 unless defined $wanted_exception; return ref($@) && $@->isa( $wanted_exception ) };

allowing you to do:

eval { $coderef->() }; if ( exception 'IOException' ) { ... handle IOException ... } elsif ( exception 'OtherException' ) { ... handle OtherException ... } elsif ( exception ) { ... handle all other exceptions ... } else { ... we lived ... };

Sure, you have an extra ref test and subroutine call, but since exceptions should be... well... exceptional it shouldn't impact your runtime speed much.


Comment on Re^2: the try/catch example from "Programming Perl" analyzed
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2015-07-28 22:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls