About use of specific error

"Boris Gärtner" Boris.Gaertner at gmx.net
Thu Mar 2 12:06:19 UTC 2006


stéphane ducasse <ducasse at iam.unibe.ch> wrote:

 
> Hi all
> 
> I asked students of my lecture to write SUnit tests for some  
> collections and I noticed that in Squeak
> the collection library do not use specific exception but instead use  
> error:.
In a Squeak3.8-5989 basic image we have 850 senders of
error:


> The consequence is that it can be tedious to capture only specific  
> error.
Yes, it is often necessary to ask an exception for its message
text to understand what happened. Checking the message texts
is of course a bit risky, they can be changed at any time,
they may be localized and so on.

> I was wondering what is the
> general feeling about this state. In VW they have various exception  
> that can be trapped more specifically.
> 

The collection relevant senders of error: are 
coded in these methods:

Object>>errorSubscriptBounds:
Object>>errorNonIntegerIndex

Collection>>errorEmptyCollection
Collection>>errorNotKeyed
Dictionary>>errorKeyNotFound
Dictionary>>errorValueNotFound
SequenceableCollection>>checkedAt:
SequenceableCollection>>errorFirstObject:
SequenceableCollection>>errorLastObject:
SequenceableCollection>>errorOutOfBounds
Set>>add:

SequenceableCollection>>with:do: 
SequenceableCollection>>with:collect:

Using specialized exceptions is attractive, but
it is not easy to find a good exception
hierarchy. What do you think about this
hierarchy for collection related errors?

Error
  CollectionAccessError
    IndexOutOfBoundsError
    KeyNotFoundError
    EmptyCollectionError

This would give you the possibility to write
handler like:

 [collection at: item]
   on: CollectionAccessError
   do:
     [:exception |
        " exception class tells you more about
          the nature of the error "
     ].


Apart form the problem of finding a good
hierarchy, we have to ask ourselves how many
exception classes we want to have. For
teaching purposes, it is a good idea to 
implement a stack with a FullStackError
and an EmptyStackError and a common
superclass  StackError, but is it really
necessary to have tenths or hundreds of
specialized error classes in a general purpose
image?

I think I would like to have specialized exceptions
for collections, but perhaps this is something one
should try to find out whether there are hidden
disadvantages.

Greetings,
Boris 



More information about the Squeak-dev mailing list