0

I have just one question.

In pycharm we get warning for except block like below

try:
    <code>
except Exception: #=> Too broad exception clause
    <code>

Just want to know apart from best practice and all, what is harm in doing so. Does it increase complexity? If there is any negative point of this please provide some official docs too.

Edit: I know my example was misleading. If I was talking about bare except block then I would have mentioned that. I have updated my example, which gives same warning.

AJAY DEVN
  • 1
  • 3
  • 1
    Does this answer your question? [What is wrong with using a bare 'except'?](https://stackoverflow.com/questions/54948548/what-is-wrong-with-using-a-bare-except) – Nick Jul 31 '22 at 16:26
  • @Nick No. I was not looking for bare except. – AJAY DEVN Jul 31 '22 at 16:36

1 Answers1

0

Do not use bare except clause. https://www.flake8rules.com/rules/E722.html will help.

You should check out PEP8 too. (https://peps.python.org/pep-0008/)

novdov
  • 11
  • 2
  • 2
  • I have updated the example. Moreover I have already mentioned in my question that I am not talking best practice/ pattern/ improve readability and all. So not looking for PEP8 style guide. I know that its not a good practice to handle broad exception. But want to know does it impact performance in any way. – AJAY DEVN Jul 31 '22 at 16:40
  • Understood. Referencing the exception in except block will simply clear warnings (e.g. print(exc)). – novdov Jul 31 '22 at 16:41
  • Yes that will remove warning. We can also remove warning by disabling check in pycharm too. But the question is more about what is the impact of that. Just for learning purpose. – AJAY DEVN Jul 31 '22 at 17:05
  • You can get a hint from PEP8. (https://peps.python.org/pep-0008/#programming-recommendations) Highlights from docs: 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. If you want to catch all exceptions that signal program errors, use except Exception: (bare except is equivalent to except BaseException:). Also you can find various examples by googling the impact of capturing bare exception. – novdov Aug 01 '22 at 07:19
  • Quest is not about bare except. For except Exception only. – AJAY DEVN Aug 01 '22 at 13:42