[Bioperl-l] Bio::Root::RootI->throw($msg,$class) overloading?
Chris Fields
cjfields at uiuc.edu
Wed Jul 12 03:27:37 UTC 2006
Makes sense to keep most of the magic in Root instead of RootI.pm.
The POD for RootI does state that the class exception thrown is
Bio::Root::NotImplemented, so we should probably either change the
POD to reflect what really happens or change throw_not_implemented
like you suggest (my vote is the latter). I don't think many (if
any) implementing classes fall into your 'trouble' category, though I
can't be sure how many actually import Bio::Root::Exception.
Chris
On Jul 11, 2006, at 8:07 PM, Steve_Chervitz wrote:
> On Jul 11, 2006, at 1:42 PM, Chris Fields wrote:
>
>> Bio::Root::Root doesn't overload throw_not_implemented from
>> Bio::Root::RootI; from the comments looks like Steve C and Ewan B
>> couldn't
>> work out some of the Error.pm issues.
>
> The issue (I believe) was that
> Bio::Root::RootI::throw_not_implemented was doing some checking for
> the presence of Error.pm and calling Error::throw. I changed it so
> that this fanciness only happens in Root.pm.
>
>> Judging by the POD for Bio::Root::RootI, throw_not_implemented
>> doesn't
>> accept arguments; it throws a Bio::Root::NotImplemented exception
>> automatically.
>
> Looking at the code now, throw_not_implemented() does not throw a
> Bio::Root::NotImplemented exception. It just throws a simple,
> unclassed message. We could allow it to throw an exception of class
> Bio::Root:NotImplemented by changing this code:
>
> if( $self->can('throw') ) {
> $self->throw($message);
> }...
>
> to this
>
> if( $self->can('throw') ) {
> $self->throw(-text=>$message, -
> class=>'Bio::Root::NotImplemented');
> }...
>
> This does not create any dependency on Error.pm, but permits it to
> be used if available. If Error.pm is not loaded, the only change is
> that the class string is included in the error message, which is
> kind of handy.
>
> Trouble would occur if the implementing class:
>
> * does not derive from Bio::Root::Root,
> * does not import Bio::Root::Exception,
> * fails to implement a method which gets called, and
> * Error.pm is available.
>
> I don't know if such implementations exist in bioperl now, but I
> suspect they would be rare (and discouraged).
>
> Steve
>
>
>> Chris
>>
>>> -----Original Message-----
>>> From: bioperl-l-bounces at lists.open-bio.org [mailto:bioperl-l-
>>> bounces at lists.open-bio.org] On Behalf Of Rutger Vos
>>> Sent: Tuesday, July 11, 2006 1:58 PM
>>> To: Chris Fields
>>> Cc: 'Bioperl List'
>>> Subject: Re: [Bioperl-l] Bio::Root::RootI->throw($msg,$class)
>>> overloading?
>>>
>>> I must have overlooked this. I think it does what I want. So
>>> could I do
>>> something like:
>>>
>>> $obj->thow_not_implemented( -class => 'Bio::Root::NotImplemented' );
>>>
>>> ...in interfaces?
>>>
>>> Chris Fields wrote:
>>>> I suppose you could; Bio::Root::Root does that using Error.pm
>>>> (if it
>>>> is installed). It almost sounds like what Bio::Root::Root does is
>>>> what you want, but you want a little more information when
>>>> exceptions
>>>> are thrown maybe?
>>>>
>>>> from perldoc Bio::Root::Root:
>>>>
>>>> ...
>>>> # 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);
>>>> ...
>>>>
>>>> Typed Exception Syntax
>>>>
>>>> The typed exception syntax of throw() has the advantage of
>>>> plainly
>>>> indicating the nature of the trouble, since the name of the
>>>> class is
>>>> included in the title of the exception output.
>>>>
>>>> To take advantage of this capability, you must specify
>>>> arguments as
>>>> named parameters in the throw() call. Here are the
>>>> parameters:
>>>>
>>>> -class
>>>> name of the class of the exception. This should be one
>>>> of the
>>>> classes defined in Bio::Root::Exception, or a custom
>>>> error of yours
>>>> that extends one of the exceptions defined in
>>>> Bio::Root::Exception.
>>>>
>>>> -text
>>>> a sensible message for the exception
>>>>
>>>> -value
>>>> the value causing the exception or $!, if appropriate.
>>>>
>>>> Note that Bio::Root::Exception does not need to be imported
>>>> into your
>>>> module (or script) namespace in order to throw
>>>> exceptions via
>>>> Bio::Root::Root::throw(), since Bio::Root::Root imports it.
>>>>
>>>>
>>>> Chris
>>>>
>>>> On Jul 11, 2006, at 3:58 AM, Rutger Vos wrote:
>>>>
>>>>
>>>>> Dear all,
>>>>>
>>>>> would it be possible to overload Bio::Root::RootI's 'throw'
>>>>> method to
>>>>> accept an additional, optional (positional) argument to define the
>>>>> exception class, e.g. using Exception::Class:
>>>>>
>>>>> # ...somewhere ...
>>>>>
>>>>> sub makefh {
>>>>> my ( $self, $filename ) = @_;
>>>>> open my $fh, '<' $filename or $self->throw("Can't open
>>>>> file: $!",
>>>>> 'Bio::Exceptions::FileIO'); # NOTE second argument
>>>>> return $fh;
>>>>> }
>>>>>
>>>>> #.... somewhere else
>>>>> my $fh;
>>>>> eval {
>>>>> $fh = $obj->makefh( 'data.txt');
>>>>> }
>>>>> if ( $@ and $@->isa('Bio::Exceptions::FileIO') ) {
>>>>> # something's wrong with the file?
>>>>> }
>>>>>
>>>>> --
>>>>> ++++++++++++++++++++++++++++++++++++++++++++++++++++
>>>>> Rutger Vos, PhD. candidate
>>>>> Department of Biological Sciences
>>>>> Simon Fraser University
>>>>> 8888 University Drive
>>>>> Burnaby, BC, V5A1S6
>>>>> Phone: 604-291-5625
>>>>> Fax: 604-291-3496
>>>>> Personal site: http://www.sfu.ca/~rvosa
>>>>> FAB* lab: http://www.sfu.ca/~fabstar
>>>>> Bio::Phylo: http://search.cpan.org/~rvosa/Bio-Phylo/
>>>>> ++++++++++++++++++++++++++++++++++++++++++++++++++++
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> Bioperl-l mailing list
>>>>> Bioperl-l at lists.open-bio.org
>>>>> http://lists.open-bio.org/mailman/listinfo/bioperl-l
>>>>>
>>>>
>>>> Christopher Fields
>>>> Postdoctoral Researcher
>>>> Lab of Dr. Robert Switzer
>>>> Dept of Biochemistry
>>>> University of Illinois Urbana-Champaign
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Bioperl-l mailing list
>>>> Bioperl-l at lists.open-bio.org
>>>> http://lists.open-bio.org/mailman/listinfo/bioperl-l
>>>>
>>>>
>>>>
>>>>
>>>
>>> --
>>> ++++++++++++++++++++++++++++++++++++++++++++++++++++
>>> Rutger Vos, PhD. candidate
>>> Department of Biological Sciences
>>> Simon Fraser University
>>> 8888 University Drive
>>> Burnaby, BC, V5A1S6
>>> Phone: 604-291-5625
>>> Fax: 604-291-3496
>>> Personal site: http://www.sfu.ca/~rvosa
>>> FAB* lab: http://www.sfu.ca/~fabstar
>>> Bio::Phylo: http://search.cpan.org/~rvosa/Bio-Phylo/
>>> ++++++++++++++++++++++++++++++++++++++++++++++++++++
>>>
>>>
>>> _______________________________________________
>>> Bioperl-l mailing list
>>> Bioperl-l at lists.open-bio.org
>>> http://lists.open-bio.org/mailman/listinfo/bioperl-l
>>
>> _______________________________________________
>> Bioperl-l mailing list
>> Bioperl-l at lists.open-bio.org
>> http://lists.open-bio.org/mailman/listinfo/bioperl-l
>
Christopher Fields
Postdoctoral Researcher
Lab of Dr. Robert Switzer
Dept of Biochemistry
University of Illinois Urbana-Champaign
More information about the Bioperl-l
mailing list