• Register
0 votes
111 views

Problem :

I got bellow message from my code

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

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

Solution:

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.

Example

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.

Example:

public class ArithmeticExceptionTest {
   public static void main(String[] args) {
      int a = 0, b = 10 ;
      int c = 0;
      try {
         c = b/a;
      } catch (ArithmeticException e) {
         e.printStackTrace();
         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 317 views
317 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 503 views
503 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 jwilliam 3.9k points
0 votes
1 answer 19 views
1 vote
1 answer 38 views
38 views
Problem: I am a begginer in java programming language and I have been coding for just some days. To practice my programming I wrote this code in java: public class Main { public static void main(String[] argv){ int 2count, h-l, a/b, } } But It shows me the error message ... , . expected Syntax error on token ",", ;expected at Main.main(Main.java:3) Whats is wrong in my code can anyone help me out?
asked Apr 6 LizzyM 6.1k points
1 vote
1 answer 16 views
16 views
Problem : I have very recently started to learn programming in Java. I am trying to execute my java code but I am facing the below error. “Exception in thread "main" java.lang.arrayindexoutofboundsexception:” I am unable to understand above error. Please find below my code: ... according to my debugger*/ myodd[j] = input[i]; j++; } return myodd; } Please help me in fixing above error.
asked Mar 30 morrisBson 3.2k points