(Exception Handling and Multithreading) a) Write a program that reads two numbers from the user to perform integer division into Num1 and Num2 variables. The division of Num1 and Num2 is displayed if they are integers. If Num1 or Num2 were not an integer, the program would throw a NumberFormatException. If Num2 were Zero, the program would throw an ArithmeticException.

//Program to throw implement division and throw exceptions
import java.util.*;
class NAexception
{
            public static void main(String args[])
            {
                        Scanner scr=new Scanner(System.in);
                        int num1,num2,q;
                        try
                        {
                                    System.out.println(“nEnter the value of first integer : “);
                                    num1=Integer.parseInt(scr.next());
                                    System.out.println(“nEnter the value of second integer : “);
                                    num2=Integer.parseInt(scr.next());
                                    q=num1/num2;
                                    System.out.println(“nQuotient is : “+q);
                        }
                        catch(NumberFormatException e)
                        {
                                    System.out.println(e);
                        }
                        catch(ArithmeticException e)
                        {
                                    System.out.println(e);
                        }
            }
}
Output:
Enter the value of first integer :
10
Enter the value of second integer :
0
java.lang.ArithmeticException: / by zero
Enter the value of first integer :
10
Enter the value of second integer :
10.0

java.lang.NumberFormatException: For input string: “10.0”

Leave a Reply

Your email address will not be published. Required fields are marked *

Enable Notifications OK No thanks