Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #112348

Re: Python Error message

From Terry Reedy <tjreedy@udel.edu>
Newsgroups comp.lang.python
Subject Re: Python Error message
Date 2016-08-04 15:36 -0400
Message-ID <mailman.176.1470339425.6033.python-list@python.org> (permalink)
References <710599b6-b8c3-4f5a-a3dd-48757b816a44@googlegroups.com> <57a36593$0$1590$c3e8da3$5496439d@news.astraweb.com> <CA+FnnTw3b-iudGYJomeUYRQo_4ZathC7E1vWVqLDGqDo74JfDA@mail.gmail.com> <23e36680-195e-b840-599e-e225cdc2cee8@mrabarnett.plus.com> <no05g6$b25$1@blaine.gmane.org>

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python Error message GBANE FETIGUE <bemen77@gmail.com> - 2016-08-04 08:31 -0700
  Re: Python Error message Chris Angelico <rosuav@gmail.com> - 2016-08-05 01:51 +1000
  Re: Python Error message Steven D'Aprano <steve+python@pearwood.info> - 2016-08-05 01:56 +1000
    Re: Python Error message Igor Korot <ikorot01@gmail.com> - 2016-08-04 12:09 -0400
    Re: Python Error message Chris Angelico <rosuav@gmail.com> - 2016-08-05 02:14 +1000
    Re: Python Error message MRAB <python@mrabarnett.plus.com> - 2016-08-04 17:19 +0100
    Re: Python Error message Terry Reedy <tjreedy@udel.edu> - 2016-08-04 15:36 -0400
    Re: Python Error message Chris Angelico <rosuav@gmail.com> - 2016-08-05 05:53 +1000
  Re: Python Error message dieter <dieter@handshake.de> - 2016-08-05 09:03 +0200

csiph-web