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
67
votes
6 answers

Why does division by zero with floating point (or double precision) numbers not throw java.lang.ArithmeticException: / by zero in Java

The following statement throws java.lang.ArithmeticException: / by zero as obvious. System.out.println(0/0); because the literal 0 is considered to be an int literal and divide by zero is not allowed in integer arithmetic. The following case…
Tiny
  • 27,221
  • 105
  • 339
  • 599
21
votes
16 answers

Is there any reason to throw a DivideByZeroException?

Are there any cases when it's a good idea to throw errors that can be avoided? I'm thinking specifically of the DivideByZeroException and ArgumentNullException For example: double numerator = 10; double denominator = getDenominator(); if(…
Armstrongest
  • 15,181
  • 13
  • 67
  • 106
14
votes
4 answers

Why does this method return double.PositiveInfinity not DivideByZeroException?

I ran the following snippet in the VS2015 C# interactive and got some very weird behavior. > double divide(double a, double b) . { . try . { . return a / b; . } . catch (DivideByZeroException exception) . { . …
Kelson Ball
  • 948
  • 1
  • 13
  • 30
8
votes
2 answers

Zero division does not throw exception in nunit

Running the following C# code through NUnit yields Test.ControllerTest.TestSanity: Expected: `` But was: null So either no DivideByZeroException is thrown, or NUnit does not catch it. Similar to this question, but…
Boris
  • 5,094
  • 4
  • 45
  • 71
6
votes
1 answer

What decides Nan and Infinity in java division operations

Output of the below code confusing me. Why NaN sometimes and Infinity other times ? public static void main (String[] args) { double a = 0.0; double b = 1.0; int c = 0; System.out.println(a/0.0); System.out.println(a/0); …
6
votes
3 answers

Why do int and decimal throw DivideByZeroException but floating point doesn't?

According to http://msdn.microsoft.com/en-us/library/system.dividebyzeroexception.aspx only Int and Decimal will throw the DivideByZeroException when you divide them by 0, but when you divide floating point by 0, the result is infinity, negative…
user2261573
  • 451
  • 2
  • 5
  • 8
4
votes
1 answer

Problem in writing "DivideByZero" Unit test case using CPPUnitTest in Visual Studio 2019 C++

Unit testing means writing code that verifies individual parts, or units, of an application or library. A unit is the smallest testable part of an application. Unit tests assess code in isolation. In C++ this means writing tests for methods or…
3
votes
3 answers

Divide by Zero Exception with Typecaste (Java)

import java.util.Scanner; public class SumDigits { public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); // prompt the user to enter a value and store it as the integer…
1
vote
1 answer

Exception handling an exception during numerical integration

I am doing a basic orbital mechanics simulation in TensorFlow. When a 'planet' gets too close to the 'sun' (when x,y is close to (0,0)), TensorFlow gets an exception during the division (which could make sense). Somehow it returns an exception…
1
vote
2 answers

How to evaluate a string expression in C# which can divide by zero?

I am facing an odd problem in C#, where I need to evaluate some mathematical string expressions, which may divide by 0. Here is an example: string expression = GetUserInput(); // Example: "(x + y) / z", where z may equal to 0 I am currently using…
Victor2748
  • 4,149
  • 13
  • 52
  • 89
1
vote
1 answer

Java Fraction Calculator - DivideByZeroMethod through a try/catch

I'm working on a program where the user inputs two different fractions and the computer then runs them through a Fraction method to test if they're equal or not. An additional step in the assignment is to throw an IllegalArgumentException to…
1
vote
2 answers

SQL Server & C# Stored Procedure & Divide by Zero Exception

So first here is my C# code and then comes the stored procedure. public DataTable GetCourseHighPass(String tmpCourse) { command.Connection = OpenConnection(); try { command.CommandText = "exec GetCourseCompletions @tmpCourse =…
1
vote
2 answers

How to ignore DivideByZeroException in Razor views

RazorEngine is used to run C# Razor views in ASP.NET MVC4 application. Views contain decimal expressions wrapped to custom Format function call like
@Format(somedecimalexpression/someotherdecimalexpression)
This causes…
Andrus
  • 26,339
  • 60
  • 204
  • 378
1
vote
2 answers

Can't see where I'm dividing by 0?

Here is all the code I think anyone would need to be able to asses my problem 1 import.java.util.Scanner 2 public class ccattano_Sieve{ 3 private boolean [] primes = new boolean [50001]; 4 private int upper; 5 private int…
Chris
  • 443
  • 5
  • 25
0
votes
0 answers

Getting ZeroDivisionError on figurecanvasTkagg on one computer but not on another [matplotlib]

I've been working a lot on my code on two different computers, using a git repository to commit all of the changes. Recently I faced a strange problem in which when using figurecanvasTkagg.draw() I get a ZeroDivisionError on one computer always,…
1
2 3