| Summary | Included libraries | Package variables | Synopsis | Description | General documentation | Methods |
# any bioperl or bioperl compliant object is a RootI
# compliant object
# Here's how to throw and catch an exception using the eval-based syntax.
$obj->throw("This is an exception");
eval {
$obj->throw("This is catching an exception");
};
if( $@ ) {
print "Caught exception";
} else {
print "no exception";
}
# Alternatively, using the new typed exception syntax in the throw() call:
$obj->throw( -class => 'Bio::Root::BadParameter',
-text => "Can't open file $file",
-value => $file);
# Exceptions can be used in an eval{} block as shown above or within
# a try{} block if you have installed the Error.pm module.
# Here's a brief example. For more, see Bio::Root::Exception
use Error qw(:try);
try {
$obj->throw( # arguments as above );
}
catch Bio::Root::FileOpenException with {
my $err = shift;
print "Handling exception $err\n";
};
try {
open( IN, $file) || $obj->throw( -class => 'Bio::Root::FileOpenException',
-text => "Cannot open file $file for reading",
-value => $!);
}
catch Bio::Root::BadParameter with {
my $err = shift; # get the Error object
# Perform specific exception handling code for the FileOpenException
}
catch Bio::Root::Exception with {
my $err = shift; # get the Error object
# Perform general exception handling code for any Bioperl exception.
}
otherwise {
# A catch-all for any other type of exception
}
finally {
# Any code that you want to execute regardless of whether or not
# an exception occurred.
};
# the ending semicolon is essential!
| BEGIN | Code | |
| _create_object | Description | Code |
| verbose | No description | Code |
| _register_for_cleanup | No description | Code |
| _unregister_for_cleanup | No description | Code |
| _cleanup_methods | No description | Code |
| throw | Description | Code |
| _create_object() | code | next | Top |
Title : _create_object() Usage : $obj->create_object(@args) Function: Method which actually creates the blessed hashref Returns : Blessed hashref Args : IgnoredOverride this method, the new() method, or _initialize() to make a custom constructor. |
| throw | code | prev | next | Top |
Title : throw
Usage : $obj->throw("throwing exception message");
or
$obj->throw( -class => 'Bio::Root::Exception',
-text => "throwing exception message",
-value => $bad_value );
Function: Throws an exception, which, if not caught with an eval or
a try block will provide a nice stack trace to STDERR
with the message.
If Error.pm is installed, and if a -class parameter is
provided, Error::throw will be used, throwing an error
of the type specified by -class.
If Error.pm is installed and no -class parameter is provided
(i.e., a simple string is given), A Bio::Root::Exception
is thrown.
Returns : n/a
Args : A string giving a descriptive error message
Named parameters:
'-class' a string for the name of a class that derives
from Error.pm, such as any of the exceptions
defined in Bio::Root::Exception.
Default class: Bio::Root::Exception
'-text' a string giving a descriptive error message
'-value' the value causing the exception, or $! (optional)
Thus, if only a string argument is given, and Error.pm is available,
this is equivalent to the arguments:
-text => "message",
-class => Bio::Root::Exception
Comments : If Error.pm is installed, and you don't want to use it
for some reason, you can block the use of Error.pm by
Bio::Root::Root::throw() by defining a scalar named
$main::DONT_USE_ERROR (define it in your main script
and you don't need the main:: part) and setting it to
a true value. |
| BEGIN | Top |
$ID = 'Bio::Root::Root'; $VERSION = 1.0; $Revision = '$Id: Root.pm,v 1.10.2.2 2002/07/01 22:38:14 sac Exp $ '; $DEBUG = 0; $VERBOSITY = 0; $ERRORLOADED = 0; # Check whether or not Error.pm is available.}
# $main::DONT_USE_ERROR is intended for testing purposes and also
# when you don't want to use the Error module, even if it is installed.
# Just put a BEGIN { $DONT_USE_ERROR = 1; } at the top of your script.
if( not $main::DONT_USE_ERROR ) { if ( eval "require Error" ) { import Error qw(:try); require Bio::Root::Exception; $ERRORLOADED = 1; $Error::Debug = 1; # enable verbose stack trace
} } if( !$ERRORLOADED ) { require Carp; import Carp qw( confess ); } $main::DONT_USE_ERROR; # so that perl -w won't warn "used only once"
| _create_object | description | prev | next | Top |
my ($class,@args) = @_; $class = ref($class) if ref($class); return bless {},$class;}
| verbose | description | prev | next | Top |
my ($self,$value) = @_; # allow one to set global verbosity flag}
if( $DEBUG ) { return $DEBUG } if(ref($self) && (defined $value || ! defined $self->{'_root_verbose'}) ) { $value = 0 unless defined $value; $self->{'_root_verbose'} = $value; } return (ref($self) ? $self->{'_root_verbose'} : $VERBOSITY);
| _register_for_cleanup | description | prev | next | Top |
my ($self,$method) = @_; if($method) { if(! exists($self->{'_root_cleanup_methods'})) { $self->{'_root_cleanup_methods'} = []; } push(@{$self->{'_root_cleanup_methods'}},$method); }}
| _unregister_for_cleanup | description | prev | next | Top |
my ($self,$method) = @_; my @methods = grep {$_ ne $method} $self->_cleanup_methods; $self->{'_root_cleanup_methods'} =\@ methods;}
| _cleanup_methods | description | prev | next | Top |
my $self = shift; my $methods = $self->{'_root_cleanup_methods'} or return; @$methods;}
| throw | description | prev | next | Top |
my ($self,@args) = @_; my ( $text, $class ) = $self->_rearrange( [qw(TEXT CLASS)], @args); if( $ERRORLOADED ) { # print STDERR " Calling Error::throw\n\n";}
# Enable re-throwing of Error objects.
# If the error is not derived from Bio::Root::Exception,
# we can't guarantee that the Error's value was set properly
# and, ipso facto, that it will be catchable from an eval{}.
# But chances are, if you're re-throwing non-Bio::Root::Exceptions,
# you're probably using Error::try(), not eval{}.
# TODO: Fix the MSG: line of the re-thrown error. Has an extra line
# containing the '----- EXCEPTION -----' banner.
if( ref($args[0])) { if( $args[0]->isa('Error')) { my $class = ref $args[0]; throw $class ( @args ); } else { my $text .= "\nWARNING: Attempt to throw a non-Error.pm object: " . ref$args[0]; my $class = "Bio::Root::Exception"; throw $class ( '-text' => $text, '-value' => $args[0] ); } } else { $class ||= "Bio::Root::Exception"; my %args; if( @args % 2 == 0 && $args[0] =~ /^-/ ) { %args = @args; $args{-text} = $text; $args{-object} = $self; } throw $class ( %args || @args ); } } else { # print STDERR " Not calling Error::throw\n\n";
$class ||= ''; my $std = $self->stack_trace_dump(); my $title = "------------- EXCEPTION $class -------------"; my $footer = "\n" . '-' x CORE::length($title); my $out = "\n$title\n" . "MSG: $text\n". $std . $footer . "\n"; die $out; }
| CONTACT | Top |
| APPENDIX | Top |