Bio::Tools::SiRNA and related moved to separate distribution
[bioperl-live.git] / examples / root / exceptions2.pl
blobb6249e54f71dd67ee24817d8dcb4abb163b7f7aa
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>
16 use strict;
18 use lib qw(lib/ ../../);
20 # Uncomment this line to force Bio::Root::Root::throw() to
21 # not use Error.pm even if it's available.
22 # Some of the tests in this script will be skipped .
23 #BEGIN { $main::DONT_USE_ERROR = 1; }
25 use Bio::Root::Root;
26 #use Bio::Root::Exception; # Not necessary since Bio::Root::Root uses it.
27 use Error qw(:try);
29 my $foo = Bio::Root::Root->new();
31 if (!$main::DONT_USE_ERROR) {
32 try {
33 # This is the new, fancier way to handle exceptions.
34 # You must have Error.pm to do this (tarball included in this dir).
36 print "[1] Throwing Error within try block via call to Bio::Root::Root::throw()\n";
37 $foo->throw( -class => 'Bio::Root::Exception',
38 -text => "Oopsie!",
39 -value => "123"
43 catch Bio::Root::Exception with {
44 my $err = shift;
45 print "[1] Caught Bio::Root::Exception:\n$err";
49 otherwise {
50 my $err = shift;
51 print "[1] Caught other Error: ", ref($err), "\n$err";
55 print "\n\n";
58 eval {
60 # This example demonstrates the traditional method for throwing
61 # an exception using Bio::Root::Root->throw('string').
62 # Notice how an exception of type Bio::Root::Exception is created.
64 print "[2] Calling Bio::Root::Root->throw('string') within an eval{}\n";
65 $foo->throw("Error message string.");
69 if($@) {
70 print "[2] Caught eval{}-based exception: ", ref($@), "\n$@";
72 else {
73 print "[2] Nothing to catch.\n";
78 print "\n\n";
80 eval {
82 # This example shows that calling Error::throw directly within
83 # an eval{} doesn't lead to a true value in $@ if
84 # the error lacks a value.
86 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";
88 if( $ENV{OSTYPE} =~ /cygwin/ ) {
89 die "[3] This causes a segmentation fault with cygwin perl! Skipping.\n";
92 throw Error::Simple ("A simple error.");
96 if($@) {
97 print "[3] Caught eval{}-based exception: ", ref($@), "\n$@\n";
99 else {
100 print "[3] Nothing to catch.\n";
104 print "\n\n";
106 eval {
108 # This example shows that calling Error::throw directly within
109 # an eval{} *does* lead to a true value in $@ if the error
110 # contains a non-zero value.
112 print "[4] Attempting to throw a valued Error within an eval{} block.\n";
114 throw Error::Simple ("A simple error.", 42);
118 if($@) {
119 print "[4] Caught eval{}-based exception: ", ref($@), "\n$@\n";
121 else {
122 print "[4] Nothing to catch.\n";
125 print "\n\n";
127 if (!$main::DONT_USE_ERROR) {
128 eval {
130 # This example shows what happens if we try to create a
131 # Bio::Root::IOException (a subclass of Bio::Root::Exception)
132 # with a zero value. Bio::Root::Exception::new() catches this
133 # faux pas and substitutes a value that will register as true in if($@).
135 print "[5] Attempting to throw a zero-valued Bio::Root::IOException\n within an eval{} block.\n";
137 throw Bio::Root::IOException ( -text =>"An error with zero value.",
138 -value => 0);
142 if($@) {
143 print "[5] Caught eval{}-based zero-valued exception: ", ref($@), "\n$@\n";
145 else {
146 print "[5] Nothing to catch.\n";
148 print "\n\n";
152 eval {
154 # If Error::throw is called *indirectly* within an eval{}
155 # (i.e., by calling a method which then calls Error::throw),
156 # $@ is defined and it consists of a reference to the Error.pm object.
158 print "[6] Attempting to throw Error indirectly within an eval{} block \n via Bio::Root::Root::throw()\n";
160 $foo->throw( -class => 'Bio::Root::Exception',
161 -text => "Oopsie!",
162 -value => "456"
167 if($@) {
168 print "[6] Caught eval{}-based exception: ", ref($@), "\n$@";
170 else {
171 print "[6] Nothing to catch.\n";
174 print "Done.\n";