Division by zero Java double does not raise an exception

I was studying Enums in Effective Java, and I was a little addicted to writing tests to see if Enums were as expected.

Enum I was writing

public enum Operator {

 // There is also PLUS.
    DIVIDE("/") {
        double apply(double x, double y) { return x / y; }
    };
    private final String symbol;
    Operator(String symbol) { this.symbol = symbol;}
    @Override public String toString() { return symbol;}
    
        abstract double apply(double x, double y);
}

Create test class for this class

public class OperatorTest {

    @Rule
    public ExpectedException thrown = ExpectedException.none();
    
    @Test
    public void zeroDivide() {

        thrown.expect(ArithmeticException.class);
       }
}

Test failed. I casually wrote a test thinking that if I divide by 0, an exception will occur. Even if you divide the double type by 0, no exception will occur.

This way it will pass.

@Test
public void zeroDivide() {
    assertThat(Operator.DIVIDE.apply(11, 0), is(Double.POSITIVE_INFINITY));
}

Recommended Posts

Division by zero Java double does not raise an exception
[java] throw an exception
Java Integer comparison (==) does not work correctly