Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Exception Handling in Perl

by nvivek (Vicar)
on Apr 27, 2010 at 04:57 UTC ( #837023=perlquestion: print w/ replies, xml ) Need Help??
nvivek has asked for the wisdom of the Perl Monks concerning the following question:

In Perl,Exception handling done by using Error.pm module.In that,there are try,catch,finally,except and some other blocks to handle exceptions.I have understood try,catch,finally blocks.I have the following doubt in except and otherwise block. When an exception is thrown,if there are no particular catch block,then it will go to except block and execute the statements in it.At the time,if I have otherwise block,that also got executed.I don't know why is it working like this.Kindly explain. For your note,if the exception caught by particular catch block,then both except and otherwise blocks aren't executed.

Comment on Exception Handling in Perl
Re: Exception Handling in Perl
by chromatic (Archbishop) on Apr 27, 2010 at 05:26 UTC

    Can you post some code? It's easier to explain what happens if so.

Re: Exception Handling in Perl
by nvivek (Vicar) on Apr 28, 2010 at 04:16 UTC

    Program is as follows.

    use Data::Dumper; #using the Dumper function to get the internal s +tructure use Error qw(:try); #specifying to going to use the try block.This + is enough to use all the # things in it try { #defining a try block my @array; #declaring an array throw Error::Simple "Oops!Array is Empty." unless(@array); #throwi +ng the exception if the array is an empty } catch Error::Simple with { #catching the error my $error=shift; #getting the Error hash print Dumper $error; #printing it in dumper function to know all t +he details such as filename,line number,the error message,package inf +ormations,etc., } except { #defining an except block print "Exception Block\n"; } otherwise { #defining an otherwise block print "Otherwise Block\n"; }
    The above program correctly throws the error in try block and it caught in the corresponding catch block.It prints the exception informations. If I commented the catch block statements in the above program,both except block and otherwise block statements are executed.This is what my doubt?

      Read the Error documentation. except is a call back that is used to help determine how to handle the exception:

      The return value from this block should be a HASHREF or a list of key-value pairs, where the keys are class names and the values are CODE references for the handler of errors of that type.

      Your test code does not make sense.

      True laziness is hard work
Re: Exception Handling in Perl
by nvivek (Vicar) on Apr 28, 2010 at 05:43 UTC
    Yeah,I know about it.I just used print statement for example.Even though,I returned key-value pairs,still it is executing otherwise block.Explain me.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2014-11-23 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (134 votes), past polls