Detect bare `raise` outside of `except` block
See original GitHub issueRule request
Thesis
Report raise
statements outside of except
block
# bad
def smth():
raise
# good
def smth():
try:
...
except:
raise
Reasoning
It causes runtime error:
>>> smth()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in smth
RuntimeError: No active exception to reraise
It is possible that the function smth
is meant to be called only from except
block and so it won’t fail but I’d say it is quite obscure and is a bad practice.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
"raise" at the end of a python function outside "try" or "except ...
A bare raise statement re-raises the last caught exception. It doesn't matter whether this is happening in a try-except block or not.
Read more >Bare "raise" statements should only be used in "except" blocks
This rule raises an exception when a bare raise statement is not in an except or finally block. Noncompliant Code Example. raise #...
Read more >Can we use a raise statement outside an except block ... - Quora
Yes, we can. Errors detected during runtime is called Exceptions. And in python, raise statement is used to raise an exception. Except block...
Read more >Reporting an error on bare "raise" outside of "except" is overly ...
Expected behaviour. Pylance should not report an error when a bare raise statement is encountered outside except . This is a valid use...
Read more >misplaced-bare-raise / E0704 - Pylint 2.15.8 documentation
Used when a bare raise is not used inside an except clause. This generates an error, since there are no active exceptions to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi. We are first-time contributors (@bhargavi247) and were wondering if we could take a look at this issue if possible? Do let us know if we can work on it. Thank you!
Please, add a new one.