Bio::Root
Exception
Summary
Bio::Root::Exception - Generic exception objects for Bioperl
Package variables
Privates (from "my" definitions)
$debug = $Error::Debug
$error = shift
$DEFAULT_VALUE = "__DUMMY__"
Synopsis
use Bio::Root::Exception;
use Error qw(:try);
$file = shift;
try {
open (IN, $file) || throw Bio::Root::FileOpenException (
-text => "Can't open file $file for reading",
-value => $!,
-object => $self );
}
catch Bio::Root::FileOpenException with {
my $err = shift;
print STDERR "Using default input file: $default_file\n";
open (IN, $default_file) || die "Can't open $default_file";
}
otherwise {
my $err = shift;
print STDERR "An unexpected exception occurred: \n$err";
# By placing an the error object reference within double quotes,
# you're invoking its stringify() method.
}
finally {
# Any code that you want to execute regardless of whether or not
# an exception occurred.
};
# the ending semicolon is essential!
# Note: You don't have to be within a try{} block
open (IN, $file) || $object->throw(
-class => 'Bio::Root::FileOpenException',
-text => "Can't open file $file for reading",
-value => $!);
@Bio::TestException::ISA = qw( Bio::Root::Exception );
Description
These are generic exceptions for typical problem situations that could arise
in any module or script.
Bio::Root::Exception()
Bio::Root::NotImplemented()
Bio::Root::IOException()
Bio::Root::FileOpenException()
Bio::Root::SystemException()
Bio::Root::BadParameter()
Bio::Root::OutOfRange()
Bio::Root::NoSuchThing()
All of these exceptions inherit from a common base class exception,
Bio::Root::Exception. This allows a user to write a handler for all
Bioperl-derived exceptions as follows:
use Bio::Whatever;
use Error qw(:try);
try {
# some code that depends on Bioperl
}
catch Bio::Root::Exception with {
my $err = shift;
print "A Bioperl exception occurred:\n$err\n";
};
The exceptions in
Bio::Root::Exception are extensions of Graham Barr's
Error.pm module available from CPAN. Despite this dependency, the
Bio::Root::Exception module does not explicitly require Error.
This permits
Bio::Root::Exception to be loaded even when
Error.pm is not available.
Error.pm is not part of the Bioperl distibution, and may not be
present within any given perl installation. So, when you want to
throw an exception in a Bioperl module, the safe way to throw it
is to use
Bio::Root::Root::throw() which can use Error.pm
when it's available. See documentation in Bio::Root::Root for details.
Methods
| new | No description | Code |
| pretty_format | No description | Code |
| _reformat_stacktrace | No description | Code |
| stringify | No description | Code |
Methods description
None available.
Methods code
sub new
{ my ($class, @args) = @_;
my %params;
if( @args % 2 == 0 ) {
%params = @args;
my $value = $params{'-value'};
if( defined $value and not $value) {
$params{-value} = "The number zero (0)" if $value == 0;
$params{-value} = "An empty string (\"\")" if $value eq "";
}
else {
$params{-value} ||= $DEFAULT_VALUE;
}
}
else {
$params{-text} ||= $args[0];
$params{-value} ||= $DEFAULT_VALUE;
}
my $self = $class->SUPER::new( %params );
return $self;} |
sub pretty_format
{ my $self = shift;
my $msg = $self->text;
my $stack = '';
if( $Error::Debug ) {
$stack = $self->_reformat_stacktrace();
}
my $value_string = $self->value ne $DEFAULT_VALUE ? "VALUE: ".$self->value."\n" : "";
my $class = ref($self);
my $title = "------------- EXCEPTION: $class -------------";
my $footer = "\n" . '-' x CORE::length($title);
my $out = $title . "\n" .
"MSG: ".$msg."\n". $value_string. $stack. $footer . "\n";
return $out;} |
sub _reformat_stacktrace
{ my $self = shift;
my $msg = $self->text;
my $stack = $self->stacktrace();
$stack =~ s/$msg//;
my @stack = split( /\n/, $stack);
my @new_stack = ();
my ($method, $file, $linenum, $prev_file, $prev_linenum);
my $stack_count = 0;
foreach my $i( 0..$#stack ) {
if( ($stack[$i] =~ /^\s*([^(]+)\s*\(.*\) called at (\S+) line (\d+)/) ||
($stack[$i] =~ /^\s*(require 0) called at (\S+) line (\d+)/)) {
($method, $file, $linenum) = ($1, $2, $3);
$stack_count++;
}
else{
next;
}
if( $stack_count == 1 ) {
push @new_stack, "STACK: $method";
($prev_file, $prev_linenum) = ($file, $linenum);
next;
}
if( $method =~ /__ANON__/ ) {
$method = "try{} block";
}
if( ($method =~ /^require/ and $file =~ /Error\.pm/ ) ||
($method =~ /^Error::subs::try/ ) ) {
last;
}
push @new_stack, "STACK: $method $prev_file:$prev_linenum";
($prev_file, $prev_linenum) = ($file, $linenum);
}
return join "\n", @new_stack;} |
sub stringify
{ my ($self, @args) = @_;
return $self->pretty_format( @args );} |
General documentation
Steve Chervitz <sac@bioperl.org>
Copyright (c) 2001 Steve Chervitz. All Rights Reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
This software is provided "as is" without warranty of any kind.
Purpose : A generic base class for all BioPerl exceptions.
By including a "catch Bio::Root::Exception" block, you
should be able to trap all BioPerl exceptions.
Example : throw Bio::Root::Exception(
-text => "A generic exception",
-object => $self );
| Methods defined by Bio::Root::Exception | Top |
new()
If you happen to specify a -value of empty string (""), it will
be replaced by the string "An empty string ("")".
| Subclasses of Bio::Root::Exception | Top |
| Bio::Root::NotImplemented | Top |
Purpose : Indicates that a method has not been implemented.
Example : throw Bio::Root::NotImplemented(
-text => "Method \"foo\" not implemented in module FooBar.",
-value => "foo" );
| Bio::Root::IOException | Top |
Purpose : Indicates that some input/output-related trouble has occurred.
Example : throw Bio::Root::IOException(
-text => "Can't save data to file $file.",
-value => $! );
| Bio::Root::FileOpenException | Top |
Purpose : Indicates that a file could not be opened.
Example : throw Bio::Root::FileOpenException(
-text => "Can't open file $file for reading.",
-value => $! );
| Bio::Root::SystemException | Top |
Purpose : Indicates that a system call failed.
Example : unlink($file) or throw Bio::Root::SystemException(
-text => "Can't unlink file $file.",
-value => $! );
| Bio::Root::BadParameter | Top |
Purpose : Indicates that one or more parameters supplied to a method
are invalid, unspecified, or conflicting.
Example : throw Bio::Root::BadParameter(
-text => "Required parameter \"-foo\" was not specified",
-value => "-foo" );
Purpose : Indicates that a specified (start,end) range or
an index to an array is outside the permitted range.
Example : throw Bio::Root::OutOfRange(
-text => "Start coordinate ($start) cannot be less than zero.",
-value => $start );
| Bio::Root::NoSuchThing | Top |
Purpose : Indicates that a requested thing cannot be located
and therefore could possibly be bogus.
Example : throw Bio::Root::NoSuchThing(
-text => "Accession M000001 could not be found.",
-value => "M000001" );