Skip to content
C

Test Exception Raising

Mediumpython

Write a function divide(a, b) that raises a ValueError when b is 0, then write a test that confirms the exception is actually raised — following pytest.raises()'s pattern using a plain try/except.

Approach: implement divide(a, b) to raise ValueError("Cannot divide by zero") when b == 0. Then call it inside a try block; if the ValueError is caught, the test passed.

Input: Two lines: a, then b (b will be 0).

Output: One line: Test passed: ValueError raised as expected (or FAIL: no exception raised if divide() doesn't raise correctly).

Input (stdin)

Output

Run your code to see output here...