| Summary | Included libraries | Package variables | Synopsis | Description | General documentation | Methods |
# Any 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 not open file $file", -value => $file ); # Want to see debug() outputs for this object my $obj = Bio::Object->new(-verbose=>1); my $obj = Bio::Object->new(%args); $obj->verbose(2); # Print debug messages which honour current verbosity setting $obj->debug("Boring output only to be seen if verbose > 0\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 | |
| new | Description | Code |
| verbose | Description | Code |
| _register_for_cleanup | No description | Code |
| _unregister_for_cleanup | No description | Code |
| _cleanup_methods | No description | Code |
| throw | Description | Code |
| debug | Description | Code |
| _load_module | Description | Code |
| DESTROY | No description | Code |
| new | code | next | Top |
Purpose : generic instantiation function can be overridden if |
| verbose | code | prev | next | Top |
Title : verbose |
| throw | code | prev | next | Top |
Title : throw |
| debug | code | prev | next | Top |
Title : debug |
| _load_module | code | prev | next | Top |
Title : _load_module |
| BEGIN | Top |
$ID = 'Bio::Root::Root';
$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 INIT { $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"
}| new | description | prev | next | Top |
# my ($class, %param) = @_;}
my $class = shift; my $self = {}; bless $self, ref($class) || $class; if(@_ > 1) { # if the number of arguments is odd but at least 3, we'll give
# it a try to find -verbose
shift if @_ % 2; my %param = @_; ## See "Comments" above regarding use of _rearrange().
$self->verbose($param{'-VERBOSE'} || $param{'-verbose'}); } return $self;
| verbose | description | prev | next | Top |
my ($self,$value) = @_; # allow one to set global verbosity flag}
return $DEBUG if $DEBUG; return $VERBOSITY unless ref $self; if (defined $value || ! defined $self->{'_root_verbose'}) { $self->{'_root_verbose'} = $value || 0; } return $self->{'_root_verbose'};
| _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; return unless ref $self && $self->isa('HASH'); 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]; $class->throw( @args ); } else { my $text .= "\nWARNING: Attempt to throw a non-Error.pm object: " . ref$args[0]; my $class = "Bio::Root::Exception"; $class->throw( '-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; } $class->throw( scalar keys %args > 0 ? %args : @args ); # (%args || @args) puts %args in scalar context!
} } 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); $text ||= ''; my $out = "\n$title\n" . "MSG: $text\n". $std . $footer . "\n"; die $out; }
| debug | description | prev | next | Top |
my ($self,@msgs) = @_; if( defined $self->verbose && $self->verbose > 0 ) { print STDERR @msgs; }}
| _load_module | description | prev | next | Top |
my ($self, $name) = @_; my ($module, $load, $m); $module = "_<$name.pm"; return 1 if $main::{$module}; # untaint operation for safe web-based running (modified after a fix}
# a fix by Lincoln) HL
if ($name !~ /^([\w:]+)$/) { $self->throw("$name is an illegal perl package name"); } else { $name = $1; } $load = "$name.pm"; my $io = Bio::Root::IO->new(); # catfile comes from IO
$load = $io->catfile((split(/::/,$load))); eval { require $load; }; if ( $@ ) { $self->throw("Failed to load module $name. ".$@); } return 1;
| DESTROY | description | prev | next | Top |
my $self = shift; my @cleanup_methods = $self->_cleanup_methods or return; for my $method (@cleanup_methods) { $method->($self); }}
| FEEDBACK | Top |
| Mailing Lists | Top |
bioperl-l@bioperl.org - General discussion
http://bioperl.org/wiki/Mailing_lists - About the mailing lists
| Reporting Bugs | Top |
http://bugzilla.open-bio.org/
| AUTHOR | Top |
| APPENDIX | Top |