Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PEP8 Does Not Allow Try Except Block [duplicate]

Tags:

python

pep8

My code contains a regular try-except block. I downloaded the pycodestyle library to test pep8 on my code. I tested my code and I got the following PEP8 error:

E722 do not use bare 'except'

Why does this happen, and how can I fix it? Thanks.

like image 812
Mr. Hax Avatar asked Jun 26 '18 03:06

Mr. Hax


People also ask

Is PEP8 deprecated?

Deprecated use of [pep8] section name in favor of [pycodestyle] ; #591. Report E722 when bare except clause is used; #579.

How do I fix broad except catching too Exception exception?

When catching exceptions, mention specific exceptions whenever possible instead of using a bare except: clause. A bare except: clause will catch SystemExit and KeyboardInterrupt exceptions, making it harder to interrupt a program with Control-C, and can disguise other problems.

What is PEP8 used for?

PEP 8, sometimes spelled PEP8 or PEP-8, is a document that provides guidelines and best practices on how to write Python code. It was written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan. The primary focus of PEP 8 is to improve the readability and consistency of Python code.


1 Answers

You should include a specific exception.

For example,

try:
   <stuff>
except IndexError:
   <stuff>

Instead of

try:
   <stuff>
except:
   <stuff>

It helps with debugging - you'll know if an unexpected error pops up, and the error won't fly by possibly messing something else up.

like image 113
whackamadoodle3000 Avatar answered Nov 15 '22 03:11

whackamadoodle3000