Skip to content

Latest commit

 

History

History
28 lines (22 loc) · 541 Bytes

TRY301.md

File metadata and controls

28 lines (22 loc) · 541 Bytes

TRY301 - raise within try

Why is it bad

Raising and catching exceptions in the same block may lead to confusion, try to abstract the raise to an inner function.

How it looks like

try:
    a = process()
    if not a:
        raise CustomException(a)
except Exception:
    logger.exception("something failed")

How it should be

try:
    a = process()  # This throws the exception now
except  CustomException:
    logger.exception("a failed")
except Exception:
    logger.exception("something failed")