fix: throw ArithmeticException on divide by zero (#9)
CI / build-and-test (pull_request) Successful in 16s

This commit is contained in:
Pit Friedrich
2026-07-25 11:15:40 +02:00
parent ab9f73ba22
commit 0ab786b24a
2 changed files with 9 additions and 0 deletions
@@ -8,6 +8,9 @@ public class DivideFunction implements CalcFunction {
}
double result = input[0];
for (int i = 1; i < input.length; i++) {
if (input[i] == 0) {
throw new ArithmeticException("Division by zero");
}
result /= input[i];
}
return result;
@@ -3,6 +3,7 @@ package com.example.util;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class DivideFunctionTest {
@@ -27,4 +28,9 @@ class DivideFunctionTest {
void dividesNegativeValues() {
assertEquals(-2.0, divideFunction.calc(4.0, -2.0));
}
@Test
void throwsOnDivideByZero() {
assertThrows(ArithmeticException.class, () -> divideFunction.calc(5.0, 0.0));
}
}