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>
19 use lib
qw(lib/ ../../);
24 # Command-line options:
25 my $eg = 0; # which example to run (a number 1-4)
26 my $help = 0; # print usage info
28 # $Error::Debug is set to true by default in Bio::Root::Interface.
29 $Error::Debug
= 1; # enables verbose stack trace
31 GetOptions
( "debug!" => \
$Error::Debug
,
36 my $options = << "OPTS";
37 -eg
1|2|3|4 Run a particular example
38 -nodebug Deactivate verbose stacktrace
42 (!$eg || $help) and die "Usage: $0 -eg 1|2|3|4 [-nodebug] [-h]\nOptions:\n$options";
44 print $Error::Debug ?
"Try a -nodebug option to supress stack trace." : "Verbose stacktrace off.";
47 # Set up a tester object.
48 my $test = TestObject
->new();
49 $test->data('Eeny meeny miney moe.');
53 test_notimplemented
( $test ) if $eg == 1;
55 test_custom_error
( $test ) if $eg == 2;
57 test_simple_error
() if $eg == 3;
59 # This subroutine doesn't even exist. But because it occurs within a try block,
60 # the Error module will create a Error::Simple to capture it. Handy eh?
62 print "Test #4: Calling an undefined subroutine.\n";
66 # We shouldn't see this stuff.
69 print "Some other code within the try block after the last throw...\n";
74 # Multiple catch blocks to handle different types of errors:
76 catch Bio
::Root
::NotImplemented with
{
78 print "\nCaught a Bio::Root::NotImplemented.\n",
79 " file : ", $error->file, "\n",
80 " line : ", $error->line, "\n",
81 " text : ", $error->text, "\n",
82 " value : ", $error->value, "\n",
83 " object: ", ref($error->object), "\n";
85 print "\nstacktrace:\n", $error->stacktrace, "\n";
87 print "\nstringify:\n$error\n";
88 # The above line is equivalent to this:
89 #print "\nstringify:\n", $error->stringify, "\n";
92 catch Bio
::TestException with
{
93 # Since we know what type of error we're getting,
94 # we can extract more information about the offending object
95 # which is retrievable from the error object.
97 print "\nCaught a Bio::TestException.\n",
98 " file : ", $error->file, "\n",
99 " line : ", $error->line, "\n",
100 " text : ", $error->text, "\n",
101 " value : ", $error->value, "\n",
102 " object: ", ref($error->object), "\n",
103 " data : ", $error->object->data, "\n";
105 print "\nstacktrace:\n", $error->stacktrace, "\n";
106 print "\nstringify:\n", $error->stringify, "\n";
111 # This is a catch-all handler for any type of error not handled above.
113 print "\nCaught an other type of error: ", ref($error), "\n",
114 " file : ", $error->file, "\n",
115 " line : ", $error->line, "\n",
116 " text : ", $error->text, "\n",
117 " value : ", $error->value, "\n",
118 " object: ", ref($error->object), "\n";
120 # print "\nstack_trace_dump:\n", $error->stack_trace_dump(), "\n";
122 print "\nstacktrace:\n", $error->stacktrace, "\n";
124 print "\nstringify:\n$error\n";
126 }; # This semicolon is essential.
130 sub test_notimplemented
{
133 # This demonstrates what will happen if a method defined in an interface
134 # that is not implemented in the implementating object.
136 print "Test #1: Inducing a Bio::Root::NotImplemented exception from TestObject\n";
142 sub test_custom_error
{
146 # TestObject::bar() deliberately throws a Bio::TestException,
147 # which is defined in TestObject.pm
149 print "Test #2: Throwing a Bio::TestException exception from TestObject\n";
156 sub test_simple_error
{
158 # Error::Simple comes with Error.pm and can have only a string and a value.
160 print "Test #3: Throwing a Error::Simple object\n";
162 throw Error
::Simple
( "A simple error", 42 );