@@ -912,8 +912,10 @@ def module_imports_on_top_of_file(
912912 Okay: # this is a comment\nimport os
913913 Okay: '''this is a module docstring'''\nimport os
914914 Okay: r'''this is a module docstring'''\nimport os
915- Okay: try:\n import x\nexcept:\n pass\nelse:\n pass\nimport y
916- Okay: try:\n import x\nexcept:\n pass\nfinally:\n pass\nimport y
915+ Okay:
916+ try:\n\timport x\nexcept ImportError:\n\tpass\nelse:\n\tpass\nimport y
917+ Okay:
918+ try:\n\timport x\nexcept ImportError:\n\tpass\nfinally:\n\tpass\nimport y
917919 E402: a=1\nimport os
918920 E402: 'One string'\n"Two string"\nimport os
919921 E402: a=1\nfrom sys import x
@@ -1179,6 +1181,22 @@ def comparison_type(logical_line, noqa):
11791181 yield match .start (), "E721 do not compare types, use 'isinstance()'"
11801182
11811183
1184+ def bare_except (logical_line , noqa ):
1185+ r"""When catching exceptions, mention specific exceptions whenever possible.
1186+
1187+ Okay: except Exception:
1188+ Okay: except BaseException:
1189+ E722: except:
1190+ """
1191+ if noqa :
1192+ return
1193+
1194+ regex = re .compile (r"except\s*:" )
1195+ match = regex .match (logical_line )
1196+ if match :
1197+ yield match .start (), "E722 do not use bare except'"
1198+
1199+
11821200def ambiguous_identifier (logical_line , tokens ):
11831201 r"""Never use the characters 'l', 'O', or 'I' as variable names.
11841202
0 commit comments