@
@
@atambalasi2020-05-18 17:47:05
Java
@atambalasi, 2020-05-18 17:47:05

Can't figure out why the last exception works?

public class Test { 
    public static void main(String[] args) { 
        int myInt = 0; 
        float myFloat = 0; 
        try { 
            float result = myFloat / myFloat; 
        } catch (RuntimeException re) { 
            System.out.println("Exception 1"); 
        } 
 
        try { 
            float result = myFloat / myInt; 
        } catch (RuntimeException re) { 
            System.out.println("Exception 2"); 
        } 
 
        try { 
            float result = myInt / myFloat; 
        } catch (RuntimeException re) { 
            System.out.println("Exception 3"); 
        } 
 
        try { 
            float result = myInt / myInt; 
        } catch (RuntimeException re) { 
            System.out.println("Exception 4"); 
        } 
    } 
}

Why is this code throwing Exception 4; Why don't the rest of the catches work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2020-05-18
_

Floating point numbers follow the IEEE-754 standard.
If v > 0 and h == 0, then v/h == INFINITY. It is guaranteed that INFINITY > 0.5.
If v < 0 and h == 0, then v/h == -INFINITY. For him -INFINITY < 0.5.
If v == 0 and h == 0, then v/h == NaN. For him, any comparison, EMNIP, returns false.
INFINITY -- infinity, its type is double/float

https://en.wikipedia.org/wiki/Division_by_zero#In_...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question