How to name Exceptions that aren't Errors

Roy Smith roy at panix.com
Thu Apr 7 22:11:48 EDT 2005


Leo Breebaart <leo at lspace.org> wrote:

> I've recently become rather fond of using Exceptions in Python to
> signal special conditions that aren't errors, but which I feel
> are better communicated up the call stack via the exception
> mechanism than via e.g. return values.
> 
> For instance, I'm thinking of methods such as:
> 
> 
>     def run(self):
>     """ Feed the input file to the simulator. """
> 
>         for linenr, line in enumerate(self.infile):
>             try:
>                 current_values = self.parse_line(linenr, line)
> ==>         except CommentLineException:
>                 continue
>             results = self.do_one_simulation_step(current_values)
>             self.process_simulation_results(results)

I know this isn't the question you asked, but I would have written a little 
generator function which hides the comment processing in a lower level:

def nonCommentLines (file):
    for lineNumber, line in enumerate (file):
        if not line.startswith('#'):
            yield lineNumber, line

for lineNumber, line in nonCommentLines (sys.stdin):
   current_values = self.parse_line(linenr, line)
   results = self.do_one_simulation_step(current_values)
   self.process_simulation_results(results)

This assumes you don't mind your line numbers starting from zero, but your 
version has the same behavior.

> My question is twofold. First, I know that many programmers are
> violently opposed to using exceptions in this fashion, i.e. for
> anything other than, well, exceptional circumstances. But am I
> correct in thinking that in Python this usage is actually
> considered quite normal, and not frowned upon? Is my snippet
> above indeed sufficiently Pythonic?

I think my code is clearer, but I wouldn't go so far as to say I'm 
violently opposed to your code.  I save violent opposition for really 
important matters like which text editor you use.

I suspect if you showed your code to a C++ guy, he might be violently 
opposed.  In C++, exceptions are perceived to be more heavyweight than they 
are in Python.  Some of this is just perception (perhaps colored by 
historical issues with older C++ compilers not handling exceptions well), 
some of it is real.  Just the other day, a colleague of mine was telling me 
about a bug in his code.  It happened because he had failed to write a copy 
constructor for an exception class he wrote.  The mind boggles how anybody 
can get anything useful done in a language like that.  But I digress.

In Python, exceptions seem to be the much more accepted way of doing 
things.  You often see:

try:
   value = dictionary[key]
except KeyError:
   whatever

instead of:

if dictionary.has_key (key):
   value = dictionary[key]
else:
   whatever

While in C++, the "if" version would be far more common.
 
> Second, purely as a question of aesthetics, I was wondering if
> the folks here might have any opinions about actual naming
> conventions for the exception classes used in this fashion.

If you look at the list of standard exceptions (import exceptions and do a 
dir() on it), you'll see things like KeyboardInterrupt, StopIteration, and 
SystemExit.  If the name fits, raise it.



More information about the Python-list mailing list