Skip to content

Remove Redundant Except Handler

Sourcery refactoring id: remove-redundant-except-handler

Description:

Removes exception handlers that can never trigger (as the exceptions have already been caught)

Before:

try:
    pass
except Exception:
    do_x()
except ValueError:
    do_y()
finally:
    do_t()

After:

try:
    pass
except Exception:
    do_x()
finally:
    do_t()

Explanation:

Currently only one except handler can be triggered for any exception raised in Python. This means that if an exception is already caught, subsequent handlers can never have an effect on it. Having them present is confusing, since the reader may think that this exception handling code is behaving as expected, when in fact it is redundant.