trapping errors

From: Simon (simalt_at_totalise.co.uk)
Date: 03/31/04


Date: Wed, 31 Mar 2004 15:21:42 +0100

How can I trap an error and let the program continue without aborting e.g

I know the die can abort on errors but not sure how to trap the error it
generates and let the program continue.

e.g the following program opens four dirs and read the contents and returns
dir count.

#!/perl/bin/perl -w

use strict;
my($dir1, $dir2, $dir3, $dir4, @file_count, $count, @hold_paths, $num,
@hold_all, $directory_numbers,

$value, $len);

# These are the directory tree paths,
$dir1 = '/tools';
$dir2 = '/test';
$dir3 = '/develop';
$dir4 = '/utils';

@hold_paths = ($dir1,$dir2,$dir3,$dir4);

$num = 0;
for(@hold_paths) { # iterate over each path, open the dir read num of files,
put into array @hold_all

        opendir(DIR, $hold_paths[$num]) || die "can't opendir
$hold_paths[$num]: $!";
        @file_count = readdir(DIR);
        $hold_all[$num] = $hold_paths[$num] ." ". scalar(@file_count) ."\n";
        closedir DIR || die "can't opendir $hold_paths[$num]: $!";
        $num++;
}

  $directory_numbers = 'dir.txt'; # will write to current directory, unless
#you put a 'path'
                                                # this is the file your
writing you data #to
  # This is where you actually write to the file
  # Will 'overwrite' file each time
  open(FILE, ">$directory_numbers") || die "can't open $hold_paths[$num]:
$!";
  print FILE @hold_all;
  close FILE || die "can't close $hold_paths[$num]: $!";
==============================================

Now what if one of the directory was not available or removed it simply
generates this message:

can't opendir /test: No such file or directory at countdirx.pl line 17.

Is there a way I can allow the program to continue even though the "test"
dir is not available but the program can report on "test" being unavailable
and show what is available.

Thanks for your help.

Simon



Relevant Pages

  • Re: trapping errors
    ... > I know the die can abort on errors but not sure how to trap the error it ... You can use eval to trap errors in your programs, but in this case, I ... here's where you test your opendir. ...
    (comp.lang.perl.misc)
  • Re: nbc 25 minutes to go
    ... They're buildin' the gallows outside my cell. ... Well, the Sheriff says, "Boy, I wanna watch you die". ... Now they're testin' the trap. ... so to hell with me. ...
    (rec.music.artists.springsteen)
  • nbc 25 minutes to go
    ... Well, the Sheriff says, "Boy, I wanna watch you die". ... Yeah, well if you're so sorry, come up and take my place. ... Now they're testin' the trap. ...
    (rec.music.artists.springsteen)
  • Re: FreeBSD 6.2-PRE panic
    ... Debug kernel and vmcore available for interested ... Fatal trap 12: page fault while in kernel mode ... abort) (CTRL-C to abort) (CTRL-C to ...
    (freebsd-stable)
  • Re: problem with XML::Parser dying on errors...
    ... some time that daemon keeps crashing on me with the message: ... XML::Parser or expat to die on my with no usable info on what is going ... You trap XML::Parser dies the same way you trap any other dies in Perl, ... by using eval BLOCK. ...
    (comp.lang.perl.misc)