maint: remove Travis stuff which has been replaced with Github actions (#325)
[bioperl-live.git] / examples / root / exceptions3.pl
blobf63aaeff9e3ab208dee1dee9c8ad0685c820fd06
1 #!/usr/bin/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 Error qw(:try);
20 use Bio::Root::Exception;
22 try {
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.
35 my $e = shift;
36 print "Caught IOException:\n\n$e";
38 finally {
39 close IN;
42 print "\nDone.\n";