Python ConfigParser: Checking for option existence

The choice between try/except and if-condition is a fuzzy line.

  1. If you expect the exception to be quite rare, use try/except as it more closely models thinking
  2. Conversely, “expected” exceptions like a configuration item missing, are part of the normal flow of control and the code should reflect that.

There is no clearly superior choice, but it sounds like you’ve got a case of (2) so I’d opt for if/then. This completely ignores aspects of Easier to ask Forgiveness Than Permission and the relative efficiencies of the structures.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)