Java- Example of handling divide by zero exception using Random class


//Example of handling divide by zero exception using  Random class

import java.util.Random;

class Test
{

public static void main(String ar[])
{
int a,b,c;

Random no=new Random();

for(int i=0;i<=32000;i++)
{
try{

a=no.nextInt();//new number generated by system each time
b=no.nextInt();
c=i/(a/b);//if divide by zero exception fires corresponding catch block will handle

System.out.println(c);

}
catch(ArithmeticException e)
{

System.out.println("don't divide number by zero");

}
}
}

}

Reactions: