How to name Exceptions that aren't Errors

Bengt Richter bokr at oz.net
Thu Apr 7 20:55:41 EDT 2005


On Thu, 07 Apr 2005 15:40:24 -0400, Steve Holden <steve at holdenweb.com> wrote:

>Leo Breebaart 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.
>> 
>Absolutely.
>
>> 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.

It also possible for exception arguments to deliver a result, rather
than indicate something rejected. E.g., you can terminate a recursive
search via an exception. If no exception occurs, you have gone through
the entire search space and not met the solution criterion.

Regards,
Bengt Richter



More information about the Python-list mailing list