question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Detect bare `raise` outside of `except` block

See original GitHub issue

Rule 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:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
amansr02commented, Apr 6, 2021

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!

1reaction
sobolevncommented, Apr 21, 2021

Please, add a new one.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found