exception handling in complex Python programs

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Thu Aug 21 09:43:24 CEST 2008


dbpokorny at gmail.com a écrit :
(snip)
> Here is an example: a simple query tool for a tiny "mock SQL"
> relational database. With a method (called "select_all") you can
> perform the equivalent of a select query on the database. The contents
> of the query are specified with triples of the form
> 
> [field, comparison_operator, value]
> 
> for instance ['name', operator.equals, cmd_name]. You can also specify
> an "order by" field which is None by default. In the code written,
> there is an assertion that the order-by field is either None or a
> valid field name (we can't order by a nonexistent field!). If the
> assertion isn't there, then I will get an error on this line:
> 
>   key_extractor = KeyExtractor(q_column_names.index(order_by_column))
> 
> In this particular case, I will get a ValueError (what does ValueError
> mean again? And what is this KeyExtractor?) since the index method
> will fail.  I wrote the tiny relational database a long time ago, and I
> really don't want to put pressure on my mental cache by thinking about
> the internal logic of this chunk of code. After scratching my head for
> a while, I'll probably figure it out. Now imagine that you instead get
> an error on this line:
> 
>   assert order_by_column in q_column_names
 >
> Now the programming error slaps me with a fish and yells "STOP! YOU
> CAN'T ORDER BY A FIELD THAT DOESN'T EXIST!!!".

As far as I'm concerned, this is a case where I would explicitely raise 
an exception (either a ValueError with an explicit message or a 
library-defined exception type).



More information about the Python-list mailing list