Divide By 0 Error In Java
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About java divide by zero error Us Learn more about Stack Overflow the company Business Learn more about hiring
Java Divide By 0 Exception
developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join divide by 0 error in sql the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Why doesn't Java throw an divide by 0 error excel Exception when dividing by 0.0? up vote 46 down vote favorite 12 I have code to calculate the percentage difference between 2 numbers - (oldNum - newNum) / oldNum * 100; - where both of the numbers are doubles. I expected to have to add some sort of checking / exception handling in case oldNum is 0. However, when I did a test run with values
Can't Divide By 0
of 0.0 for both oldNum and newNum, execution continued as if nothing had happened and no error was thrown. Running this code with ints would definitely cause an arithmetic division-by-zero exception. Why does Java ignore it when it comes to doubles? java types integer double divide-by-zero share|improve this question edited Oct 1 '12 at 9:46 Aziz Shaikh 11.5k73753 asked Mar 4 '10 at 17:57 froadie 24k46117190 2 Good question - the inconsistency between integer and double behavior adds confusion and hassle. –Steve B. Mar 4 '10 at 18:09 1 possible duplicate of Why does division by zero with floating point (or double precision) numbers not throw java.lang.ArithmeticException: / by zero in Java –Raedwald Jan 21 '14 at 21:35 1 @Raedwald - considering that this question was posted 2 1/2 years before the one you linked, I would say that question is a (possible) duplicate of this one :) –froadie Jan 22 '14 at 10:24 add a comment| 4 Answers 4 active oldest votes up vote 30 down vote accepted The result of division by zero is, mathematically speaking, undefined, which can be expressed with a float/double (as NaN - not a numb
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn java divide by zero infinity more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags how to catch divide by zero exception in java Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like
Divide By Zero Exception In C++ Program
you, helping each other. Join them; it only takes a minute: Sign up Java division by zero doesnt throw an ArithmeticException - why? up vote 14 down vote favorite 4 Why this code doesn't throw an ArithmeticException? Take http://stackoverflow.com/questions/2381544/why-doesnt-java-throw-an-exception-when-dividing-by-0-0 a look: public class NewClass { public static void main(String[] args) { // TODO code application logic here double tab[] = {1.2, 3.4, 0.0, 5.6}; try { for (int i = 0; i < tab.length; i++) { tab[i] = 1.0 / tab[i]; } } catch (ArithmeticException ae) { System.out.println("ArithmeticException occured!"); } } } I have no idea! java arithmeticexception share|improve this question edited Jan 3 '13 at 12:16 mtk 5,560103482 asked Jan 3 '13 at 11:25 http://stackoverflow.com/questions/14137989/java-division-by-zero-doesnt-throw-an-arithmeticexception-why Katie 73371836 so how can I change my code to get an ArithmeticException? (I dont want to change the type of an array to int)? –Katie Jan 3 '13 at 11:30 3 This is duplicated stackoverflow.com/questions/5291606/… –Seba Jan 3 '13 at 11:30 2 if (tab[i] == 0) throw new ArithmeticException();. –assylias Jan 3 '13 at 11:31 @assylias: Im not so sure about that, I read somewhere that we shouldnt throw an ArithmerticException –Katie Jan 3 '13 at 11:32 1 Katie, you are overthinking. :) assylias solution is just fine. If, for some reason, you don't want to throw ArithmeticException, just throw the exception you want. –gd1 Jan 3 '13 at 11:34 | show 3 more comments 5 Answers 5 active oldest votes up vote 11 down vote accepted Why can't you just check it yourself and throw an exception if that is what you want. try { for (int i = 0; i < tab.length; i++) { tab[i] = 1.0 / tab[i]; if (tab[i] == Double.POSITIVE_INFINITY || tab[i] == Double.NEGATIVE_INFINITY) throw new ArithmeticException(); } } catch (ArithmeticException ae) { System.out.println("ArithmeticException occured!"); } share|improve this answer answered Jan 3 '13 at 11:36 Waqas Ilyas 2,416522 3 You need to check for Double.isNaN() as well. –Peter Lawrey Jan 3 '13 at 12:34 add a comment| up vote 42 d
SOAEJB3SpringPDFEmailJ2MEJ2EE ApplicationXMLDesign PatternLogSecurityApache CommonAntJUnitcatch divide-by-zero error : try http://www.java2s.com/Tutorial/Java/0080__Statement-Control/catchdividebyzeroerror.htm catch«Statement Control«Java TutorialJava https://www.tutorialspoint.com/javaexamples/exception_multiple.htm TutorialStatement Controltry catchpublic class MainClass { public static void main(String args[]) { int d, divide by a; try { d = 0; a = 42 / d; System.out.println("This will not be printed."); } catch divide by 0 (ArithmeticException e) { // System.out.println("Division by zero."); } System.out.println("After catch statement."); } } 4.10.try catch4.10.1.catch divide-by-zero error4.10.2.Handle an exception and move on.4.10.3.Demonstrate multiple catch statements.4.10.4.Catch different Exception types4.10.5.An example of nested try statements.4.10.6.Try statements can be implicitly nested via calls to methodsjava2s.com |Email:info at java2s.com|© Demo Source and Support. All rights reserved.
- Arrays Example - Date & Time Example - Methods Example - Files Example - Directories Example - Exceptions Example - Data Structure Example - Collections Example - Networking Example - Threading Example - Applets Example - Simple GUI Example - JDBC Example - Regular Exp Java Tutorial Java - Tutorial Java Useful Resources Java - Quick Guide Java Useful Resources Selected Reading Developer's Best Practices Questions and Answers Effective Resume Writing HR Interview Questions Computer Glossary Who is Who Java Examples - Multiple exception (divided by zero) Advertisements Previous Page Next Page Problem Description: How to use handle multiple exceptions (devided by zero)? Solution: This example shows how to handle multiple exceptions while deviding by zero? public class Main { public static void main (String args[]) { int array[]={20,20,40}; int num1=15,num2=0; int result=0; try { result = num1/num2; System.out.println("The result is" +result); for(int i =2;i 7gt;=0; i--){ System.out.println ("The value of array is" +array[i]); } } catch (ArrayIndexOutOfBoundsException e) { System.out.println ("Error�. Array is out of Bounds"+e); } catch (ArithmeticException e) { System.out.println ("Can't be divided by Zero"+e); } } } Result: The above code sample will produce the following result. Can't be divided by Zerojava.lang.ArithmeticException: / by zero Previous Page Print PDF Next Page java_exceptions.htm Advertisements Write for us FAQ's Helping Contact © Copyright 2016. All Rights Reserved. go