2023-07-03 09:48:41 +02:00
This rule raises an issue when a bare `raise` statement is not in an `except` or `finally` block.
2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-07-03 09:48:41 +02:00
A bare `raise` statement, i.e. a `raise` with no exception provided, will re-raise the last active exception in the current scope:
[source,python]
----
def foo():
try:
...
except ValueError as e:
raise # this will re-raise "e"
----
If the `raise` statement is not in an `except` or `finally` block, no exception is active and a `RuntimeError` is raised instead.
2021-04-28 16:49:39 +02:00
2023-07-03 09:48:41 +02:00
If the bare `raise` statement is in a function called in an `except` block, the exception caught by the `except` will be re-raised. However, this behavior is not reliable as nothing prevents a developer from calling the function from a different context.
2021-04-28 16:49:39 +02:00
2023-07-03 09:48:41 +02:00
Overall, having bare `raise` statements outside of `except` blocks is discouraged as it is hard to understand and maintain.
2021-04-28 16:49:39 +02:00
2023-07-03 09:48:41 +02:00
=== Notes
2021-04-28 16:49:39 +02:00
2023-07-03 09:48:41 +02:00
In a `finally` block, an exception is still active only when it hasn't been caught in a previous `except` clause or if it has been raised in an `except` block. In both cases, it is better to let the exception propagate automatically than to re-raise it. This pattern is covered by rule S5704.
2021-04-28 16:49:39 +02:00
2023-07-03 09:48:41 +02:00
== How to fix it
2021-04-28 16:49:39 +02:00
2023-07-03 09:48:41 +02:00
To fix this issue, make sure to specify which exception needs to be raised when outside of an `except` block.
2021-04-28 18:08:03 +02:00
2023-07-03 09:48:41 +02:00
=== Code examples
2021-04-28 16:49:39 +02:00
2023-07-03 09:48:41 +02:00
==== Noncompliant code example
[source,python,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
2023-07-03 09:48:41 +02:00
raise # Noncompliant: no active exception
2021-04-28 16:49:39 +02:00
def foo():
2023-07-03 09:48:41 +02:00
raise # Noncompliant: no active exception
2021-04-28 16:49:39 +02:00
try:
2023-07-03 09:48:41 +02:00
raise # Noncompliant: no active exception
except ValueError:
2021-04-28 16:49:39 +02:00
handle_error()
def handle_error():
2023-07-03 09:48:41 +02:00
raise # Noncompliant: this is not reliable
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-07-03 09:48:41 +02:00
==== Compliant solution
2021-04-28 16:49:39 +02:00
2023-07-03 09:48:41 +02:00
[source,python,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
raise ValueError()
def foo():
raise ValueError()
try:
raise ValueError()
2023-07-03 09:48:41 +02:00
except ValueError:
2021-04-28 16:49:39 +02:00
raise
----
2023-05-03 11:06:20 +02:00
== Resources
2021-04-28 16:49:39 +02:00
2023-07-03 09:48:41 +02:00
=== Documentation
2021-04-28 16:49:39 +02:00
* Python Documentation - https://docs.python.org/3/reference/simple_stmts.html#raise[The ``++raise++`` statement]
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Remove this "raise" statement or move it inside an "except" block.
=== Highlighting
The raise statement
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== relates to: S5704
=== relates to: S5706
=== is related to: S1039
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]