This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: When to make an Exception of an Exception?
Feed Title: Travis Griggs - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/travis-rss.xml
Feed Description: This TAG Line is Extra
With the ANSI exception stuff, the exception hierarchy is a good old class hierarchy. On my system, I get the following:
Class
withAllSubclasses size
GenericException
200
Exception
189
Error
148
So, a good chunk of the classical exception set is put under Error. Here's the dilemma. When do you just use Error, and when do you make your own new Error sub type? The pro of just using Error is that you don't have to do anything new really. You just start using
self error: 'someErrorString'
The drawback is that if you need to trap and handle your special error, you have to handle it ambiguously with something like:
[self doMyThing] on: Error do: [:ex | self dealWithMySpecificError]
But what if there's a ZeroDivide signal that happens in there? It'll trap that too. Generally, I frown on trying to handle specific errors by trapping any type.
Apparently, this is a dilemma for the base image authors as well. Take Dictionary for example. It inherits from Collection four error raising messages:
emptyCollectionError
noMatchError
notFoundError
notKeyedError
It adds to that, two of its own:
keyNotFoundErrorFor:index:
valueNotFoundError:
The interesting thing about these 6 errors is that 3 of them raise specific errors (sub types of Error) and 3 of them don't. Can you guess which three? I couldn't. If you had to pick the three that deserved their own type of error, what would you choose. I ran into this, because I wanted to trap the EmptyCollectionError, but there isn't one. It's just a generic Error. Even more interesting to note is that the three that do raise their own signals all use different forms and APIs of actually getting the exception raised!
So this leaves me wondering, when I'm designing my own set of exceptions to raise, with an example like that, where do I look for examples of a good way to do it? What APIs to use? And when, and when not to just hijack Error instead of creating your own new one? What do others do? It makes me wonder what a newbies exposure to an "Exceptional" Smalltalk session is like.
(P.S: There are 725 senders of #error: in my system)