Skip to content

Files

Latest commit

 

History

History
31 lines (22 loc) · 698 Bytes

bad-except-order.md

File metadata and controls

31 lines (22 loc) · 698 Bytes

Pattern: Wrong order for except clauses

Issue: -

Description

Used when except clauses are not in the correct order (from the more specific to the more generic). If you don't fix the order, some exceptions may not be caught by the most specific handler.

Example of incorrect code:

try:
    __revision__ += 1
except LookupError:
    __revision__ = -1
except IndexError: # [bad-except-order]
    __revision__ = -2

Here warning is raised because LookupError is a base class for IndexError and is specified first. Example of correct code:

try:
    __revision__ += 1
except IndexError:
    __revision__ = -1
except LookupError:
    __revision__ = -2