Sobes.tech
Junior — Middle

How can you explicitly trigger an exception in negative testing?

sobes.tech AI

Answer from AI

In Python, the raise operator is used for explicitly raising an exception. In negative testing, this allows verifying that the code correctly handles errors and exceptional situations.

Example:

def divide(a, b):
    if b == 0:
        raise ValueError("Division by zero is not possible")
    return a / b

try:
    divide(10, 0)
except ValueError as e:
    print(f"Caught exception: {e}")

Here, we explicitly raise a ValueError to test the handling of division by zero error.