How to name Exceptions that aren't Errors

Leo Breebaart leo at lspace.org
Thu Apr 7 15:23:21 EDT 2005


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)


which I use in order to discard comments from a file I'm parsing
line-by-line.

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?

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.

'CommentLineError' would clearly be wrong, but using the
'Exception' prefix seems a bit redundant and pointless too. I
suppose I could just call the exception "CommentLine" and leave
it at that, but I don't know, maybe there's something better I'm
overlooking.

Any suggestions?

-- 
Leo Breebaart  <leo at lspace.org>



More information about the Python-list mailing list