maint: remove Travis stuff which has been replaced with Github actions (#325)
[bioperl-live.git] / examples / root / exceptions2.pl
blobbcb19cded20d15424aad2eac55f2ca9927b573fc
1 #!/usr/bin/perl
3 # This shows how Error.pm-based objects can be thrown
4 # by Bio::Root::Root::throw() when Error.pm is available.
5 # When Error.pm isn't available, Bio::Root::Root::throw()
6 # works as usual.
8 # It also demonstrates what happens when you use an outer eval{}
9 # instead of a try{} to trap thrown Error.pm-based exceptions.
10 # The behavior is the same as when Error.pm is not used.
11 # This is important for backward compatibility.
13 # Author: Steve Chervitz <sac@bioperl.org>
16 use strict;
19 # Uncomment this line to force Bio::Root::Root::throw() to
20 # not use Error.pm even if it's available.
21 # Some of the tests in this script will be skipped .
22 #BEGIN { $main::DONT_USE_ERROR = 1; }
24 use Bio::Root::Root;
25 #use Bio::Root::Exception; # Not necessary since Bio::Root::Root uses it.
26 use Error qw(:try);
28 my $foo = Bio::Root::Root->new();
30 if (!$main::DONT_USE_ERROR) {
31 try {
32 # This is the new, fancier way to handle exceptions.
33 # You must have Error.pm to do this (tarball included in this dir).
35 print "[1] Throwing Error within try block via call to Bio::Root::Root::throw()\n";
36 $foo->throw( -class => 'Bio::Root::Exception',
37 -text => "Oopsie!",
38 -value => "123"
42 catch Bio::Root::Exception with {
43 my $err = shift;
44 print "[1] Caught Bio::Root::Exception:\n$err";
48 otherwise {
49 my $err = shift;
50 print "[1] Caught other Error: ", ref($err), "\n$err";
54 print "\n\n";
57 eval {
59 # This example demonstrates the traditional method for throwing
60 # an exception using Bio::Root::Root->throw('string').
61 # Notice how an exception of type Bio::Root::Exception is created.
63 print "[2] Calling Bio::Root::Root->throw('string') within an eval{}\n";
64 $foo->throw("Error message string.");
68 if($@) {
69 print "[2] Caught eval{}-based exception: ", ref($@), "\n$@";
71 else {
72 print "[2] Nothing to catch.\n";
77 print "\n\n";
79 eval {
81 # This example shows that calling Error::throw directly within
82 # an eval{} doesn't lead to a true value in $@ if
83 # the error lacks a value.
85 print "[3] Attempting to throw a valueless Error within an eval{} block\n (this should fail to be caught by Error.pm v0.13 but is caught by v0.14 and greater).\n";
87 if( $ENV{OSTYPE} =~ /cygwin/ ) {
88 die "[3] This causes a segmentation fault with cygwin perl! Skipping.\n";
91 throw Error::Simple ("A simple error.");
95 if($@) {
96 print "[3] Caught eval{}-based exception: ", ref($@), "\n$@\n";
98 else {
99 print "[3] Nothing to catch.\n";
103 print "\n\n";
105 eval {
107 # This example shows that calling Error::throw directly within
108 # an eval{} *does* lead to a true value in $@ if the error
109 # contains a non-zero value.
111 print "[4] Attempting to throw a valued Error within an eval{} block.\n";
113 throw Error::Simple ("A simple error.", 42);
117 if($@) {
118 print "[4] Caught eval{}-based exception: ", ref($@), "\n$@\n";
120 else {
121 print "[4] Nothing to catch.\n";
124 print "\n\n";
126 if (!$main::DONT_USE_ERROR) {
127 eval {
129 # This example shows what happens if we try to create a
130 # Bio::Root::IOException (a subclass of Bio::Root::Exception)
131 # with a zero value. Bio::Root::Exception::new() catches this
132 # faux pas and substitutes a value that will register as true in if($@).
134 print "[5] Attempting to throw a zero-valued Bio::Root::IOException\n within an eval{} block.\n";
136 throw Bio::Root::IOException ( -text =>"An error with zero value.",
137 -value => 0);
141 if($@) {
142 print "[5] Caught eval{}-based zero-valued exception: ", ref($@), "\n$@\n";
144 else {
145 print "[5] Nothing to catch.\n";
147 print "\n\n";
151 eval {
153 # If Error::throw is called *indirectly* within an eval{}
154 # (i.e., by calling a method which then calls Error::throw),
155 # $@ is defined and it consists of a reference to the Error.pm object.
157 print "[6] Attempting to throw Error indirectly within an eval{} block \n via Bio::Root::Root::throw()\n";
159 $foo->throw( -class => 'Bio::Root::Exception',
160 -text => "Oopsie!",
161 -value => "456"
166 if($@) {
167 print "[6] Caught eval{}-based exception: ", ref($@), "\n$@";
169 else {
170 print "[6] Nothing to catch.\n";
173 print "Done.\n";