bad-exception-cause / E0705ΒΆ

Message emitted:

Exception cause set to something which is not an exception, nor None

Description:

Used when using the syntax "raise ... from ...", where the exception cause is not an exception, nor None.

Problematic code:

def divide(x, y):
    result = 0
    try:
        result = x / y
    except ZeroDivisionError:
        # +1: [bad-exception-cause]
        raise ValueError(f"Division by zero when dividing {x} by {y} !") from result
    return result

Correct code:

def divide(x, y):
    result = 0
    try:
        result = x / y
    except ZeroDivisionError as exc:
        raise ValueError(f"Division by zero when dividing {x} by {y} !") from exc
    return result

Related links:

Created by the exceptions checker.