diff --git a/pythonmaths/arithmetic.py b/pythonmaths/arithmetic.py index a26c2db..9869b50 100644 --- a/pythonmaths/arithmetic.py +++ b/pythonmaths/arithmetic.py @@ -51,8 +51,15 @@ def divide(x: int | float, y: int | float) -> float: 5.0 >>> arithmetic.divide(5, 2) 2.5 + + I've added a try and except code block to catch a divide by zero error. """ - return x / y + try: + return x / y + except ZeroDivisionError as e: + raise ZeroDivisionError( + "You can not divide by 0, please choose another value for 'y'." + ) from e def multiply(x: int | float, y: int | float) -> float: diff --git a/tests/test_arithmetic.py b/tests/test_arithmetic.py index f3d46b8..fa2c01a 100644 --- a/tests/test_arithmetic.py +++ b/tests/test_arithmetic.py @@ -64,3 +64,10 @@ def test_multiply(x: int | float, y: int | float, expected: int | float) -> None def test_divide(x: int | float, y: int | float, expected: int | float) -> None: """Test the divide function.""" assert arithmetic.divide(x, y) == pytest.approx(expected) + +def test_divide_zero_division_exception() -> None: + """Test that a ZeroDivisionError is raised by the divide() function.""" + with pytest.raises(ZeroDivisionError): + arithmetic.divide(2, 0) + +