java.lang.ArithmeticException example in Java

In this Program i am going to do division of two numbers in Java.

Error Message:
Enter a number1
10
Enter a number2
0
Exception in thread "main" java.lang.ArithmeticException: / by zero
       at com.lib.Divisionoftwonumbers.main(Divisionoftwonumbers.java:14)


Error Description:

If you are going to divide by zero(0) we will get an ArithematicException.

package com.lib;

import java.util.Scanner;

public class Divisionoftwonumbers {

       public static void main(String[] args)
       {
              Scanner sc = new Scanner(System.in);
              System.out.println("Enter a number1");
              int num1 = sc.nextInt();
              System.out.println("Enter a number2");
              int num2 = sc.nextInt();
              int temp = num1/num2;

             
       }

}



Error Solution:

package com.lib;

import java.util.Scanner;

public class Divisionoftwonumbers {

       public static void main(String[] args)
       {
              Scanner sc = new Scanner(System.in);
              System.out.println("Enter a number1");
              int num1 = sc.nextInt();
              System.out.println("Enter a number2");
              int num2 = sc.nextInt();
              int temp = num1/num2;
              System.out.println(temp);

       }

}


Output:
Enter a number1
10
Enter a number2
2
5
Share this post :

Post a comment

Please share your valuable feedback and share this article in social media.

 
Support : Ranga Rajesh Kumar
Copyright © 2019. Error Solution - All Rights Reserved
Site Designed by Ranga Rajesh Kumar