Why not allow empty code blocks?

Steven D'Aprano steve at pearwood.info
Fri Jul 29 10:38:32 EDT 2016


On Fri, 29 Jul 2016 11:55 pm, Antoon Pardon wrote:

> Op 29-07-16 om 15:43 schreef Steven D'Aprano:
>> Of course it won't, which is why I don't believe all these folks who
>> claim that they regularly ("all the time", "fairly often") replace except
>> blocks with `pass`. I call shenanigans -- perhaps you do it
>> *occasionally*, but as a general rule, you can rarely replace the
>> exception handler with a do-nothing clause and expect your code to work:
>>
>> try:
>>     block
>> except SomeException:
>>     handle error
>> process
>>
>> If you remove the error handling block, replacing it with `pass`, in
>> general your code will just break again as soon as it continues
>> processing.
> 
> I think the case where you just want to ignore the exception, but it can
> at times be useful to get some extra logging information for debuging
> purposes, is not that rare as you seem to suggest.

I think I can count the number of times I've wanted to ignore an exception
on the fingers of one hand. And when I did, there was no need to debug it,
because I only ignore expected failures. There's nothing to debug.

But even if you do it all the time, how do you "get some extra logging
information" by replacing the "except" block with `pass`? If you replaced
the "except" block with a call to the logger, that would be one thing.
But "pass", that won't do the job.




-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list