Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Exception Handling in Perl

by nvivek (Vicar)
on Apr 28, 2010 at 04:16 UTC ( #837218=note: print w/ replies, xml ) Need Help??

in reply to Exception Handling in Perl

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?

Comment on Re: Exception Handling in Perl
Download Code
Replies are listed 'Best First'.
Re^2: Exception Handling in Perl
by GrandFather (Sage) on Apr 28, 2010 at 04:51 UTC

    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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2015-11-27 16:49 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (731 votes), past polls