3 # A simple tester script for demonstrating how to throw and catch
4 # Error.pm objects. It also shows how to define new types of
5 # Error.pm-based objects.
7 # It relies on the tester modules TestObject.pm and TestInterface.pm
8 # which you should also look at.
10 # Note that Bio::Root::NotImplemented is a subclass of Error.pm
11 # and is defined in Bio::Root::Exception.pm
13 # This code requires Graham Barr's Error.pm module available from CPAN.
15 # Author: Steve Chervitz <sac@bioperl.org>
23 # Command-line options:
24 my $eg = 0; # which example to run (a number 1-4)
25 my $help = 0; # print usage info
27 # $Error::Debug is set to true by default in Bio::Root::Interface.
28 $Error::Debug
= 1; # enables verbose stack trace
30 GetOptions
( "debug!" => \
$Error::Debug
,
35 my $options = << "OPTS";
36 -eg
1|2|3|4 Run a particular example
37 -nodebug Deactivate verbose stacktrace
41 (!$eg || $help) and die "Usage: $0 -eg 1|2|3|4 [-nodebug] [-h]\nOptions:\n$options";
43 print $Error::Debug ?
"Try a -nodebug option to supress stack trace." : "Verbose stacktrace off.";
46 # Set up a tester object.
47 my $test = TestObject
->new();
48 $test->data('Eeny meeny miney moe.');
52 test_notimplemented
( $test ) if $eg == 1;
54 test_custom_error
( $test ) if $eg == 2;
56 test_simple_error
() if $eg == 3;
58 # This subroutine doesn't even exist. But because it occurs within a try block,
59 # the Error module will create a Error::Simple to capture it. Handy eh?
61 print "Test #4: Calling an undefined subroutine.\n";
65 # We shouldn't see this stuff.
68 print "Some other code within the try block after the last throw...\n";
73 # Multiple catch blocks to handle different types of errors:
75 catch Bio
::Root
::NotImplemented with
{
77 print "\nCaught a Bio::Root::NotImplemented.\n",
78 " file : ", $error->file, "\n",
79 " line : ", $error->line, "\n",
80 " text : ", $error->text, "\n",
81 " value : ", $error->value, "\n",
82 " object: ", ref($error->object), "\n";
84 print "\nstacktrace:\n", $error->stacktrace, "\n";
86 print "\nstringify:\n$error\n";
87 # The above line is equivalent to this:
88 #print "\nstringify:\n", $error->stringify, "\n";
91 catch Bio
::TestException with
{
92 # Since we know what type of error we're getting,
93 # we can extract more information about the offending object
94 # which is retrievable from the error object.
96 print "\nCaught a Bio::TestException.\n",
97 " file : ", $error->file, "\n",
98 " line : ", $error->line, "\n",
99 " text : ", $error->text, "\n",
100 " value : ", $error->value, "\n",
101 " object: ", ref($error->object), "\n",
102 " data : ", $error->object->data, "\n";
104 print "\nstacktrace:\n", $error->stacktrace, "\n";
105 print "\nstringify:\n", $error->stringify, "\n";
110 # This is a catch-all handler for any type of error not handled above.
112 print "\nCaught an other type of error: ", ref($error), "\n",
113 " file : ", $error->file, "\n",
114 " line : ", $error->line, "\n",
115 " text : ", $error->text, "\n",
116 " value : ", $error->value, "\n",
117 " object: ", ref($error->object), "\n";
119 # print "\nstack_trace_dump:\n", $error->stack_trace_dump(), "\n";
121 print "\nstacktrace:\n", $error->stacktrace, "\n";
123 print "\nstringify:\n$error\n";
125 }; # This semicolon is essential.
129 sub test_notimplemented
{
132 # This demonstrates what will happen if a method defined in an interface
133 # that is not implemented in the implementating object.
135 print "Test #1: Inducing a Bio::Root::NotImplemented exception from TestObject\n";
141 sub test_custom_error
{
145 # TestObject::bar() deliberately throws a Bio::TestException,
146 # which is defined in TestObject.pm
148 print "Test #2: Throwing a Bio::TestException exception from TestObject\n";
155 sub test_simple_error
{
157 # Error::Simple comes with Error.pm and can have only a string and a value.
159 print "Test #3: Throwing a Error::Simple object\n";
161 throw Error
::Simple
( "A simple error", 42 );