Bio/DB/Taxonomy/flatfile.pm: Fix for issue #80, changed DESTROY
[bioperl-live.git] / examples / root / exceptions3.pl
bloba8e8b86e930322217f4f7a47fecb66eb89b8006a
1 #!/usr/bin/env perl
3 # This shows that Error objects can be subclassed into more specialized types.
4 # Bio::Root::FileOpenException is a subclass of Bio::Root::IOException.
6 # We can write a generic handler to trap any type of IOException
7 # or we could handle FileOpenExceptions explicitly.
9 # To demo, run this script without any arguments, then try it with an argument
10 # that doesn't correspond to any file on your system (e.g., foobar).
11 # Then try running with a valid file name.
13 # This requires Graham Barr's Error.pm module available from CPAN.
15 # Author: Steve Chervitz <sac@bioperl.org>
18 use strict;
19 use lib qw(lib/ ../../);
20 use Error qw(:try);
21 use Bio::Root::Exception;
23 try {
24 print "Starting try block.\n";
25 my $file = shift @ARGV || throw Bio::Root::IOException(-text=>"No file supplied.");
27 open ( IN, $file) || throw Bio::Root::FileOpenException(-text=>"Can't open file \"$file\"", -value=> $!);
29 print "Opened file $file\n";
32 catch Bio::Root::IOException with {
33 # This handler deals with IOException or any of its subclasses.
34 # We could also write a handler with a `catch Bio::Root::FileOpenException'.
35 # Such a handler would appear before this one.
36 my $e = shift;
37 print "Caught IOException:\n\n$e";
39 finally {
40 close IN;
43 print "\nDone.\n";