Remove Redundant Except Handler
Documentation Index
Fetch the complete documentation index at: https://docs.sourcery.ai/llms.txt
Use this file to discover all available pages before exploring further.
Sourcery refactoring id: remove-redundant-except-handler
Section titled “Sourcery refactoring id: remove-redundant-except-handler”Description:
Section titled “Description:”Removes exception handlers that can never trigger (as the exceptions have already been caught)
Before:
Section titled “Before:”try: passexcept Exception: do_x()except ValueError: do_y()finally: do_t()After:
Section titled “After:”try: passexcept Exception: do_x()finally: do_t()Explanation:
Section titled “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.
Related Rules
Section titled “Related Rules”- merge-except-handler to merge except handlers with the same content
- remove-redundant-exception to clean up the tuple of an except clause
- use-contextlib-suppress instead of empty
exceptblocks - do-not-use-bare-except to always specify which
errors an
exceptblock is handling