Questions tagged [arithmeticexception]

An ArithmeticException is an Exception that indicates an error generated when performing a calculation.

An ArithmeticException is an Exception that indicates an error generated when performing a calculation.

A common calculation that throws this exception is trying to divide a number by zero, such as this...

double firstNumber = 10;
double secondNumber = input.readDouble();
double result = firstNumber / secondNumber;

When coding such a calculation, it is easy to overlook the fact that the user can input a zero for the secondNumber. For the vast majority of numbers, this code will run correctly, and so the programmer might overlook the potential problem.

Programmers should wrap calculation code within try-catch blocks to help catch these Exceptions, or perform validation of variables before they're used in a calculation.

64 questions
0
votes
5 answers

Java Exception Handling understanding issue

I'm not able to understand this program. I expect it to output "Hello World", but instead it prints only the "World". I thought that first the try block would execute, printing "Hello" and " ", then afterward when it encounters a 1/0, it would…
Sandeep Roy
  • 417
  • 6
  • 27
0
votes
0 answers

Android strange ArithmeticException : Divide by Zero

I'm getting an ArithmeticException (Divide by 0) on a few devices in my activity. This is the Stack trace I'm getting java.lang.ArithmeticException: divide by zero at com.afre.quirky.GameActivity.a(Unknown Source) at…
Vishal Afre
  • 1,013
  • 3
  • 12
  • 39
0
votes
2 answers

gdb arithmetic exception - no division by zero - runtime error

In order to get the mean value of a disparity map I had to write an own method to ignore negative disparity values: float Utility::calcMeanDisparity(cv::Mat const& matrix) { int total = 0; int numElements = 0; for(int r = 0; r < matrix.rows;…
hGen
  • 2,235
  • 6
  • 23
  • 43
0
votes
2 answers

ArithmeticException division by zero... how to fix this method?

The purpose of this method is to iterate through a 2D array of integers called grid[][], and translate the integers based on the maximum and minimum values into a smaller range between 100 and 250 (the original minimum value becomes 100, the…
0
votes
3 answers

Project Euler #3 out of integer range java

The code is supposed to give back the biggest prime number. More about the task here: https://projecteuler.net/problem=3 int checkFactors(double na) { long n = (long) na; int biggestPrimeFactor = 0; for (int i = 1; i < n;…
ByteBiter
  • 116
  • 1
  • 7
0
votes
1 answer

Java Applet Returns "java.lang.ArithmeticException: / by zero"

What I am doing: In the code snippet below I am printing out the X Axis of a bar chart which is dependent on the amount of words of different lengths. E.g. the largest word is length 6 so the X axis has 1,2,3,4,5,6, along the bottom. What the issue…
bradleyduncan
  • 49
  • 2
  • 10
0
votes
1 answer

Reading from ODBC reader - Arithmetic Overflow Exception

I am trying to retrieve data via an ODBC connection and put it in a datatable. The data coming in might have some cell values null. Here is the code that I am using to import the data in a datatable: DataTable dt = new DataTable(); OdbcCommand cmd…
James Gatt
  • 86
  • 1
  • 3
  • 15
-1
votes
2 answers

How to solve ArithmeticException Error : / by zero

import java.util.Scanner; public class InputCalculator { public static void main(String[] args) { inputThenPrintSumAndAverage(); } public static void inputThenPrintSumAndAverage(){ Scanner scanner = new…
Sean Lim
  • 19
  • 3
-1
votes
1 answer

java.lang.RuntimeException - Caused by: java.lang.ArithmeticException

Here are two play console logs and I think both are same issue. I used https://github.com/z3r0c00l-2k/AquaDroid this library in my app which is on play store. But I do not know how to solve this crash which is happening. please someone help me, also…
kunal
  • 133
  • 1
  • 1
  • 9
-1
votes
2 answers

How to execute Arithmetic Exception catch block in Java?

This is the code snippet: public void actionPerformed(ActionEvent e){ int i1,i2; try{ if(e.getSource()==b1){ . . . else if(e.getSource()==b4){ double…
user7554107
-1
votes
1 answer

Can this expression throw exception

This is one of the lines I am having in C# app override of WndProc: int hiWord = unchecked((short)((long)m.LParam >> 16)); if (hiWord != 0) { // do something } Is it possible that it may throw Arithmetic exception?
Leon Havin
  • 177
  • 2
  • 14
-1
votes
1 answer

Arithmetic exception

Here is my Java code. I am facing an error "Fatal Exception main" I want my app to run successfully. Please help me. I am new to Android if(queList.size() == question_Counter+1) { // answerList = new ArrayList(); final…
user5732936
-1
votes
2 answers

Why customize Exception doesn't catch this? (JAVA)

look at the next code lines please: public void methodBla(){ try{ system.out.println(2/0); { catch(MyArithmeticException me){ system.out.println("Error: My exception"); } catch(Exception a){ …
Ziv Ru
  • 11
  • 2
-1
votes
2 answers

What's the most efficient way to check if the program is dividing a variable by 0?

Let's assume x is the divisor and it may possibly by 0. The program runs this: Scanner reader = new Scanner(System.in); int y = 5; int x = 0; x = reader.nextInt(); System.out.println("y divided by x is " + y/x); I know that if a program finds…
Angelo Rivera
  • 121
  • 1
  • 1
  • 11
-2
votes
2 answers

Arithmetic Exception divide by zero : but i am already restricting it not to produce zero

here is my sample code, logcat is showing exception in while statement, I am out of mind now. I don't know how and why it is being divided by zero. Help me please : public void divisionQuestion() { int a; int b; Random random = new…
gagangamer
  • 14
  • 1
  • 6