2023-05-03 11:06:20 +02:00
== Why is this an issue?
2021-01-27 13:42:22 +01:00
Exceptions handlers (``++except:++``) are evaluated in the order they are written. Once a match is found, the evaluation stops.
2020-06-30 12:47:33 +02:00
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
In some contexts an except block is dead code as it will never catch any exception:
2020-06-30 14:49:38 +02:00
2020-06-30 12:47:33 +02:00
* If there is a handler for a base class followed by a handler for class derived from that base class, the second handler will never trigger: The handler for the base class will match the derived class, and will be the only executed handler.
2021-01-27 13:42:22 +01:00
* When multiple ``++except++`` statements try to catch the same exception class, only the first one will be executed.
* In python 3, ``++BaseException++`` is the parent of every exception class. When ``++BaseException++`` is caught and the same try-except block has a bare ``++except:++`` statement, i.e. an ``++except++`` with no expression, the bare except will never catch anything.
2020-06-30 12:47:33 +02:00
2021-01-27 13:42:22 +01:00
This rule raises an issue when an ``++except++`` block catches every exception before a later ``++except++`` block could catch it.
2020-06-30 12:47:33 +02:00
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2020-06-30 12:47:33 +02:00
----
def foo():
try:
raise FloatingPointError()
except (ArithmeticError, RuntimeError) as e:
print(e)
except FloatingPointError as e: # Noncompliant. FloatingPointError is a subclass of ArithmeticError
print("Never executed")
except OverflowError as e: # Noncompliant. OverflowError is a subclass of ArithmeticError
print("Never executed")
try:
raise TypeError()
except TypeError as e:
print(e)
except TypeError as e: # Noncompliant. Duplicate Except.
print("Never executed")
try:
raise ValueError()
except BaseException as e:
print(e)
except: # Noncompliant. This is equivalent to "except BaseException" block
print("Never executed")
----
2023-05-03 11:06:20 +02:00
=== Compliant solution
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2020-06-30 12:47:33 +02:00
----
def foo():
try:
raise FloatingPointError()
except FloatingPointError as e:
print("Executed")
except OverflowError as e:
print("Executed")
except (ArithmeticError, RuntimeError) as e:
print(e)
try:
raise TypeError()
except TypeError as e:
print(e)
try:
raise ValueError()
except BaseException as e:
print(e)
----
2023-05-03 11:06:20 +02:00
== Resources
2020-06-30 12:47:33 +02:00
2021-01-27 13:42:22 +01:00
* Python Documentation - https://docs.python.org/3/reference/compound_stmts.html#the-try-statement[The ``++try++`` statement]
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
=== Highlighting
Primary:
* All exceptions which will never match because they are subclasses or duplicate of the first exception caught.
* The "except:" statement if "BaseException" is caught before.
Secondary:
* The exception class which shadows other except blocks.
message should be: 'Exceptions will be caught here.'
=== Message
* When a parent except block is caught before derived classes
* Catch this exception only once; it is already handled by a previous except clause.
* When the same exception class is caught multiple times:
* Catch this exception only once; it is already handled by a previous except clause.
* When an "BaseException" is caught in the same try-except as a bare "except:"
* Merge this bare "except:" with the "BaseException" catch
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)
include::../comments-and-links.adoc[]
2023-06-22 10:38:01 +02:00
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]