• Register
0 votes

Problem :

I got bellow message from my code

Exception in thread "main" java.lang.ArithmeticException: / by zero
6 5 3
6,930 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

Usually one would come across “java.lang.ArithmeticException: / by zero” when tried to divide two numbers and the number in the denominator is zero.

Handling the ArithmeticException using try-catch block.

1. Firstly Surround the statements that could throw ArithmeticException with try-catch block.

2. Catch ArithmeticException

3. When any exception occurs, the execution falls onto the catch block from the point of occurrence of exception. 

4. It executes the statement in catch block and continues with the statement present after the try-catch block. 

5. So care has to be taken while handling the exceptions.

9 7 4
38,600 points
0 votes


The java.lang.ArithmeticException is an unchecked exception in Java. Generally, one would meet up java.lang.ArithmeticException: / by zero which happens at the time an try is made to divide two numbers and the number in the denominator is zero. ArithmeticException objects may be created by the JVM.


public class ArithmeticExceptionTest {
   public static void main(String[] args) {
      int a = 0, b = 10;
      int c = b/a;
      System.out.println("Value of c is : "+ c);

ArithmeticExeption is happened since denominator value is zero.

  • java.lang.ArithmeticException: Exception thrown by java at the time of division.

  • / by zero: is the detail message provided to ArithmeticException class while making the ArithmeticException object.

Let us handle the ArithmeticException employing try and catch blocks.

  • Enclose the statements that can throw ArithmeticException with try and catch blocks.
  • Also, we can Catch the ArithmeticException
  • Take requires action for our program, as the execution doesn’t abort.


public class ArithmeticExceptionTest {
   public static void main(String[] args) {
      int a = 0, b = 10 ;
      int c = 0;
      try {
         c = b/a;
      } catch (ArithmeticException e) {
         System.out.println("We are just printing the stack trace.\n"+ "ArithmeticException is handled. But take care of the variable \"c\"");
      System.out.println("Value of c :"+ c);

At the time of an exception happens, the execution falls to the catch block from the point of instance of an exception. It executes the statement in the catch block and maintain with the statement present after the try and catch blocks.

java.lang.ArithmeticException is a type of Exception that tagget to better describe what the problem really is. There's no point onluy making an Exception, as it truly could be happened by anything. By originating an ArithmeticException, the user can instantly know that the issue is something to do with a estimation. An Exception can be funded by any piece of code, adding in estiminations such as your example.

Exception in thread "main" refers that the exception is thrown by the main() method, which also occurs to be the primary Thread that is running your code.

List of all invalid operations that entrusted an ArithmeticException() in Java

  • Dividing by an integer Zero
  • Non-terminating decimal numbers employing BigDecimal

Dividing by an integer Zero:

Java funded an Arithmetic exception at the time a calculation try is done to divide by zero, where the zero is an integer. Take the conforming piece of code as an example:

package co.java.exception;
public class ArithmaticExceptionEx 
   void divide(int a,int b)
      int q=a/b;
      System.out.println("Sucessfully Divided");
      System.out.println("The Value After Divide Is :-" +q);
   public static void main(String[] args) 
      ArithmaticExceptionEx obj=new ArithmaticExceptionEx();
      obj.divide(10, 0);


10 6 4
31,120 points

Related questions

0 votes
1 answer 518 views
Problem : In my code have main method in it. The program compiles on jgrasp with no errors but it keeps giving me a message as bellow: "no main methods, applets, or Midlets found in file jgrasp"
asked Nov 1, 2019 peterlaw 6.9k points
0 votes
1 answer 815 views
Problem : I want to input some of the details about student to my program. But when I press a save button its giving me the below listed exception error: "Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet."
asked Jan 31, 2020 jwilliam 3.9k points
0 votes
1 answer 4 views
Thank you in advance for any help?
asked Jan 5 TeamScript 9.2k points
0 votes
1 answer 8 views
Help me by solving this problem.
asked Jan 2 TeamScript 9.2k points
0 votes
1 answer 10 views
I know the problem lies with the recursion.
asked Jan 2 TeamScript 9.2k points
0 votes
1 answer 8 views
Who can help me to solve this problem?
asked Dec 28, 2020 TeamScript 9.2k points
0 votes
1 answer 10 views
0 votes
1 answer 16 views
Can you please tell me the root cause?
asked Dec 26, 2020 TeamScript 9.2k points
0 votes
1 answer 24 views
explain with an example?
asked Nov 18, 2020 jagruthi 680 points
0 votes
1 answer 46 views