Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Exception handling - try Catch Method

by Anonymous Monk
on Aug 27, 2004 at 02:42 UTC ( #386228=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I would like to open a file and if can not open, need to throw a message using Exception handling method(try-catch method).
How can i do that? I have written like this in normal method, how can i convert it try-catch method?
open (FH, "directory/myfile") || die "Can Not open the file"; @array = <FH>; close(FH);

Can anyone post a solution here

Replies are listed 'Best First'.
Re: Exception handling - try Catch Method
by Zaxo (Archbishop) on Aug 27, 2004 at 03:02 UTC

    use Errno; OPEN: eval { open FH, '<', 'directory/myfile' or die $!; }; if ($@) { { $@ == EACCES and die $@; $@ == ENFILE and sleep(1), goto OPEN; warn $@; } }
    There are interesting definitions of try and catch to be seen around the monastery, but they all boil down to the construction I show.

    After Compline,

Re: Exception handling - try Catch Method
by csuhockey3 (Curate) on Aug 27, 2004 at 04:14 UTC
Re: Exception handling - try Catch Method
by Rakeswell (Initiate) on Aug 27, 2004 at 05:42 UTC
    Try this:
    eval {open (FH, "directory/myfile") || die "Can Not open the file ($!) +";}; if ($@){ # handle your exception # maybe check contents of $@ to decide what to do } @array = <FH>; close(FH);
Re: Exception handling - try Catch Method
by ybiC (Prior) on Aug 27, 2004 at 03:30 UTC
    /me asks ignorantly...

    I've seen try-catch stuff in my Java(TM) dabblings, but it seems awkwARd and saskwAAtch.   Why, pray tell, might one *choose* to do it this way?


      striving toward Perl Adept
      (it's pronounced "why-bick")
Re: Exception handling - try Catch Method
by reneeb (Chaplain) on Aug 27, 2004 at 06:20 UTC
      Read Re: Re2: Learning how to use the Error module by example before recommending Error.

      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: Exception handling - try Catch Method
by Matts (Deacon) on Aug 28, 2004 at 16:02 UTC
    Along with the suggestions here, don't forget to take a look at the Fatal module (run "perldoc Fatal" on the command line) to see how to have open() automatically throw an exception when it fails to open the file. This automates what I consider most of the hard work of exceptions in perl (i.e. actually remembering to throw them!), and also includes useful error text in the exception.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://386228]
Approved by kvale
Front-paged by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2021-01-22 14:03 GMT
Find Nodes?
    Voting Booth?