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>
20 use Bio
::Root
::Exception
;
23 print "Starting try block.\n";
24 my $file = shift @ARGV || throw Bio
::Root
::IOException
(-text
=>"No file supplied.");
26 open ( IN
, $file) || throw Bio
::Root
::FileOpenException
(-text
=>"Can't open file \"$file\"", -value
=> $!);
28 print "Opened file $file\n";
31 catch Bio
::Root
::IOException with
{
32 # This handler deals with IOException or any of its subclasses.
33 # We could also write a handler with a `catch Bio::Root::FileOpenException'.
34 # Such a handler would appear before this one.
36 print "Caught IOException:\n\n$e";