File: exceptions3.pl

package info (click to toggle)
bioperl 1.7.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 35,788 kB
  • sloc: perl: 94,019; xml: 14,811; makefile: 20
file content (42 lines) | stat: -rw-r--r-- 1,277 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/perl

# This shows that Error objects can be subclassed into more specialized types.
# Bio::Root::FileOpenException is a subclass of  Bio::Root::IOException. 
#
# We can write a generic handler to trap any type of IOException
# or we could handle FileOpenExceptions explicitly.
#
# To demo, run this script without any arguments, then try it with an argument
# that doesn't correspond to any file on your system (e.g., foobar). 
# Then try running with a valid file name.
#
# This requires Graham Barr's Error.pm module available from CPAN.
#
# Author: Steve Chervitz <sac@bioperl.org>
#

use strict;
use Error qw(:try);
use Bio::Root::Exception;

try {
   print "Starting try block.\n";
   my $file = shift @ARGV || throw Bio::Root::IOException(-text=>"No file supplied.");

   open ( IN, $file) || throw Bio::Root::FileOpenException(-text=>"Can't open file \"$file\"", -value=> $!); 

   print "Opened file $file\n";

}
catch Bio::Root::IOException with {
    # This handler deals with IOException or any of its subclasses.
    # We could also write a handler with a `catch Bio::Root::FileOpenException'.
    # Such a handler would appear before this one.
    my $e = shift;
    print "Caught IOException:\n\n$e";
}
finally {
     close IN;
};

print "\nDone.\n";