Resolve conflict (can't directly merge since trunk and branch have slightly different...
[bioperl-live.git] / examples / root / exceptions2.pl
blob9007127766ae1223d3a69bf88320be24489b4c7a
1 #!/usr/bin/env 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>
15 # $Id$
17 use strict;
19 use lib qw(lib/ ../../);
21 # Uncomment this line to force Bio::Root::Root::throw() to
22 # not use Error.pm even if it's available.
23 # Some of the tests in this script will be skipped .
24 #BEGIN { $main::DONT_USE_ERROR = 1; }
26 use Bio::Root::Root;
27 #use Bio::Root::Exception; # Not necessary since Bio::Root::Root uses it.
28 use Error qw(:try);
30 my $foo = Bio::Root::Root->new();
32 if (!$main::DONT_USE_ERROR) {
33 try {
34 # This is the new, fancier way to handle exceptions.
35 # You must have Error.pm to do this (tarball included in this dir).
37 print "[1] Throwing Error within try block via call to Bio::Root::Root::throw()\n";
38 $foo->throw( -class => 'Bio::Root::Exception',
39 -text => "Oopsie!",
40 -value => "123"
44 catch Bio::Root::Exception with {
45 my $err = shift;
46 print "[1] Caught Bio::Root::Exception:\n$err";
50 otherwise {
51 my $err = shift;
52 print "[1] Caught other Error: ", ref($err), "\n$err";
56 print "\n\n";
59 eval {
61 # This example demonstrates the traditional method for throwing
62 # an exception using Bio::Root::Root->throw('string').
63 # Notice how an exception of type Bio::Root::Exception is created.
65 print "[2] Calling Bio::Root::Root->throw('string') within an eval{}\n";
66 $foo->throw("Error message string.");
70 if($@) {
71 print "[2] Caught eval{}-based exception: ", ref($@), "\n$@";
73 else {
74 print "[2] Nothing to catch.\n";
79 print "\n\n";
81 eval {
83 # This example shows that calling Error::throw directly within
84 # an eval{} doesn't lead to a true value in $@ if
85 # the error lacks a value.
87 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";
89 if( $ENV{OSTYPE} =~ /cygwin/ ) {
90 die "[3] This causes a segmentation fault with cygwin perl! Skipping.\n";
93 throw Error::Simple ("A simple error.");
97 if($@) {
98 print "[3] Caught eval{}-based exception: ", ref($@), "\n$@\n";
100 else {
101 print "[3] Nothing to catch.\n";
105 print "\n\n";
107 eval {
109 # This example shows that calling Error::throw directly within
110 # an eval{} *does* lead to a true value in $@ if the error
111 # contains a non-zero value.
113 print "[4] Attempting to throw a valued Error within an eval{} block.\n";
115 throw Error::Simple ("A simple error.", 42);
119 if($@) {
120 print "[4] Caught eval{}-based exception: ", ref($@), "\n$@\n";
122 else {
123 print "[4] Nothing to catch.\n";
126 print "\n\n";
128 if (!$main::DONT_USE_ERROR) {
129 eval {
131 # This example shows what happens if we try to create a
132 # Bio::Root::IOException (a subclass of Bio::Root::Exception)
133 # with a zero value. Bio::Root::Exception::new() catches this
134 # faux pas and substitutes a value that will register as true in if($@).
136 print "[5] Attempting to throw a zero-valued Bio::Root::IOException\n within an eval{} block.\n";
138 throw Bio::Root::IOException ( -text =>"An error with zero value.",
139 -value => 0);
143 if($@) {
144 print "[5] Caught eval{}-based zero-valued exception: ", ref($@), "\n$@\n";
146 else {
147 print "[5] Nothing to catch.\n";
149 print "\n\n";
153 eval {
155 # If Error::throw is called *indirectly* within an eval{}
156 # (i.e., by calling a method which then calls Error::throw),
157 # $@ is defined and it consists of a reference to the Error.pm object.
159 print "[6] Attempting to throw Error indirectly within an eval{} block \n via Bio::Root::Root::throw()\n";
161 $foo->throw( -class => 'Bio::Root::Exception',
162 -text => "Oopsie!",
163 -value => "456"
168 if($@) {
169 print "[6] Caught eval{}-based exception: ", ref($@), "\n$@";
171 else {
172 print "[6] Nothing to catch.\n";
175 print "Done.\n";