Python Error message

MRAB python at mrabarnett.plus.com
Thu Aug 4 12:19:57 EDT 2016


On 2016-08-04 17:09, Igor Korot wrote:
> Steven,
>
> On Thu, Aug 4, 2016 at 11:56 AM, Steven D'Aprano
> <steve+python at 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.
>




More information about the Python-list mailing list