Questions tagged [dividebyzeroexception]

An exception that occurs when a calculation attempts to divide a number by zero, which has an undefined result.

An exception that occurs when a calculation attempts to divide a number by zero, which has an undefined result.

This could occur as a result of many different actions, such as...

  1. You're using a variable which hasn't been initialised, or has a default value of zero
  2. The user has input a value which wasn't expected (such as text input to a numerical field), or the user entered a zero value

Your code should be able to prevent or handle this type of exception, if you're doing divisions that involve variable inputs...

  1. Detect - Before performing the calculation, check the value of the variables to ensure they aren't zero. If they are, perform an alternate operation, such as alerting the user of an error.
  2. Prevent - If the variable is being input from the user, restrict the users input to a range of values. For example, only allow numerical values to be entered, and numerical values should be >= 1. If the field can only accept a range of values like this, you can't end up with a zero in your calculation.
  3. Handle - Wrap your calculation in exception-handling code, such as a try-catch block. This will allow you to catch unexpected exceptions such as this, and perform an alternate operation, such as alerting the user of the error.
36 questions
0
votes
2 answers

Arithmetic error : / 0

I can't see what I'm doing wrong I'm new to processing and keeps giving me this error. It says this is the error code: int sampleCount = (int) ((byteCount - 36) / (bitDepth * channels)); this is the full class it's from : public…
0
votes
4 answers

C++ program crashes when counter is used inside loop

I am working on a very small program to find the divisors of an integer in C++. My main method pretty much cins an int to a var and calls the factor method with the int as the argument. Here's the code: void factor(int num) { for(int x = 0; x <…
the_pwner224
  • 99
  • 1
  • 1
  • 11
0
votes
1 answer

Divide By Zero doesn't throw DivideByZeroException with float or double...why?

I've been playing with some Math and I noticed that instead of throwing a DivideByZeroException floats and doubles get assigned the value Infinitity when you divide by zero Why is this the case? [TestMethod] public void…
Peter
  • 7,792
  • 9
  • 63
  • 94
0
votes
1 answer

DivideByZeroException in Webrequest test (WP7)

I am using NUnit for testing WP7 application (Install-Package NUnit). I created blank WP Class project with such test: var req = WebRequest.CreateHttp("http://google.com"); There is such error in this test: System.TypeInitializationException : The…
Anton Kandybo
  • 3,678
  • 4
  • 23
  • 31
-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
-6
votes
1 answer

Divide by zero error when calculating frequency?

I have a file containing 2 columns: time & volts. I'm trying to calculate the frequency of the highest amplitude wave. (Bear in mind that Each wave is of fixed frequency) I'm ordering the file in Excel using the volts column in descending order,…
1 2
3