try-except with no exceptions

Ben Finney ben+python at benfinney.id.au
Thu Oct 13 14:40:59 EDT 2016


Daiyue Weng <daiyueweng at gmail.com> writes:

> I am wondering how to correct the code above (what it tries to do is
> basically trying one processing block, if not working, running another
> block of code in except). Also a warning 'Too broad exception clause'
> will be generated.

Yes. You need to be *very* clear about which specific “not working”
conditions you expect to handle, and catch only those.

Also, having identified which specific conditions you will handle, you
need to break complex statements and expressions into simpler ones, and
only place reduce the ‘try’ block to only those statements which will
encounter those conditions. Move everything else outside the ‘try …
except …’ altogether.

-- 
 \         “In prayer, it is better to have a heart without words than |
  `\                         words without heart.” —Mohandas K. Gandhi |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list