-1

In this programe MyclassEx is child class of Exception(java.lang.Exception).we create user define exception.In the class contain a parameterized constructor who pass its argument to super class that is Exception class.outside this class I declare another class SimpleThrow.here i take input of two variable x,y of double type.and Z is divident of x,y.if i take y as zero it should throw the control to catch(ArithmeticException e)block.but it do not throw any of the catch block except the user defined Exception i.e,MyclassEx.How can I make all catch block active with the Myclass exception?

import java.lang.Exception;
import java.util.*;
class MyclassEx extends Exception{
    MyclassEx(String msg)
    {
        super(msg);
        System.out.println(msg);
    }
}
public class SimpleThrow
{
    public static void main(String[] args)
    {
        
        try
        {
            double x=0,y=0,z=0;
            Scanner sc=new Scanner(System.in);
            System.out.print("Enter two numbers:");
            x=sc.nextDouble();
            y=sc.nextDouble();
            z=x/y;
                
                if(z<0.01)
                {
                    **throw new MyclassEx("Number is too small");**
                }  
        }
        catch(MyclassEx e)
        {
            System.out.println("caught exception");
                
        }
        catch(ArithmeticException obj)
        {
            System.out.println("division not possible BY 0");
        }
        catch(InputMismatchException e2)
        {
            System.out.println("you have entered wrong key:");
        }
        finally
        {
            System.out.println("yoo");
        }
    }
}
D:\debolina\java\Error_handling>javac SimpleThrow.java

D:\debolina\java\Error_handling>java SimpleThrow       
Enter two numbers:45
0
yoo
Jens
  • 67,715
  • 15
  • 98
  • 113
  • You might want to look at https://stackoverflow.com/questions/14137989/java-division-by-zero-doesnt-throw-an-arithmeticexception-why – Progman Jan 15 '22 at 21:00

1 Answers1

0

If y is 0 then z is Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY (depend on x sign). No Exception is thrown. In your example, z is Double.POSITIVE_INFINITY that it is greater than 0.01, so no MyClassEx exception is thrown.