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


Groups > comp.lang.python > #112337

Re: Python Error message

From MRAB <python@mrabarnett.plus.com>
Newsgroups comp.lang.python
Subject Re: Python Error message
Date 2016-08-04 17:19 +0100
Message-ID <mailman.167.1470327601.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>

Show all headers | View raw


On 2016-08-04 17:09, Igor Korot wrote:
> Steven,
>
> On Thu, Aug 4, 2016 at 11:56 AM, Steven D'Aprano
> <steve+python@pearwood.info> wrote:
>> On Fri, 5 Aug 2016 01:31 am, GBANE FETIGUE wrote:
>>
>>> try:
>>>   parsed_response = json.loads(response)
>>>   deployid  = parsed_response[u'id']
>>>   print "Your deployid is: " + deployid
>>> except:
>>>     print 'Seems the named id  already exists!'
>>
>>
>> I'm not going to try to debug your code blindfolded with my hands tied
>> behind my back. Get rid of those "try...except" blocks so that you can see
>> what error is *actually* happening.
>>
>> As you have it now, an error happens, somewhere. You don't know where the
>> error is, or what it is, but Python generates a nice exception showing all
>> the detail you need to debug.
>>
>> But you catch that exception, throw it away, and then print a lie.
>>
>> It is **not true** that the named ID already exists. That is not what the
>> error is, so why does your script tell a lie?
>>
>> The output from the server might give you a clue:
>>
>> "The server refused this request because the request entity is in a format
>> not supported by the requested resource for the requested method."
>>
>>
>> Never[1] use a bare "try...except". It is the worst thing you can do to a
>> Python script, making it almost impossible to debug.
>>
>> https://realpython.com/blog/python/the-most-diabolical-python-antipattern/
>>
>> Fix that problem first, get rid of the "try...except" and lying print
>> messages, and then either the bug will be obvious, or we can debug further.
>>
>>
>>
>>
>>
>>
>> [1] There are exceptions to this rule, for experts. But if you need to ask
>> what they are, you're not ready to know
>
> But even the experts will never write such a code - you never know what happens
> in a month. Server might throw some new exception, you may move on to
> a different project,
> etc, etc. ;-)
>
In those rare occasions when you do write a bare except, you'd re-raise 
the exception afterwards:

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

> Thank you.
>

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