Python Error message

Terry Reedy tjreedy at udel.edu
Thu Aug 4 15:36:38 EDT 2016


On 8/4/2016 12:19 PM, MRAB wrote:

> In those rare occasions when you do write a bare except,

A bare "except:" is never needed and in my opinion, and that of others, 
one should never write one (except possibly for experimentation). Be 
explicit and write "except BaseException:" or "except Exception:", 
whichever one is the actual intent.

 > you'd re-raise the exception afterwards:

As a general rule, this is wrong, just as this rule is wrong for other 
exception blocks.

> try:
>     ...
> except:
>     print("'tis but a scratch!")
>     raise

This is right when one wants to do something *in addition to* the normal 
handling, such as log errors to a file, but is wrong when wants to do 
something *instead of* allowing the normal handling.

An example of the latter is when one writes code in Python to execute 
'other' code.  (IDLE is one example.  It both executes user statements 
and evals user expressions.)  One needs "except BaseException:"  to 
isolate the interpreter from exceptions raised in the interpreted code. 
(It would be wrong for IDLE to stop because a user submitted code that 
raises, whether intentionally or accidentally)  A 'raise' that throws 
the exception into the interpreter is likely the worst thing to do.

-- 
Terry Jan Reedy




More information about the Python-list mailing list