Java Division By 0 Error
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 Us Learn more about Stack Overflow the company Business
Java Divide By Zero Exception Handling
Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation java divide by zero infinity Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just how to catch divide by zero exception in java like you, helping each other. Join them; it only takes a minute: Sign up How should I throw a divide by zero exception in Java without actually dividing by zero? up vote 18 down vote favorite 2
Divide By Zero Exception In C++ Program
I have an I2C device that wants two inputs: a denominator and a numerator. Both are written to separate addresses, so no actual calculation (numerator/denominator) is done. The problem with this is that a divide by zero could occur on the I2C device, so a divide by zero error needs to be checked for. Ideally, exactly the same thing would happen if the dividing were done by the java code. At the moment, I've bodged
Divide By Zero Exception In C#
an unused variable that does the division, but I'm worried it'll get optimized out: public void setKp(int numerator, int divisor) { int zeroCheck = numerator / divisor; //... doesn't use zeroCheck } Surely there's a better way! java exception math divide-by-zero share|improve this question asked Nov 1 '09 at 18:27 Eric 51.2k24114226 The answers suggesting checking divisor directly seem better, but I can't imagine this getting optimized away. Sure the zeroCheck might not get assigned, but the division has a potential to throw an exception, so I can't imagine it actually being optimized away. –Yishai Nov 1 '09 at 18:35 add a comment| 5 Answers 5 active oldest votes up vote 34 down vote accepted You should not throw an ArithmeticException. Since the error is in the supplied arguments, throw an IllegalArgumentException. As the documentation says: Thrown to indicate that a method has been passed an illegal or inappropriate argument. Which is exactly what is going on here. if (divisor == 0) { throw new IllegalArgumentException("Argument 'divisor' is 0"); } share|improve this answer answered Nov 1 '09 at 18:38 Joren 10.2k13548 The OP specified: "Ideally, exactly the same thing would happen if the dividing were done by the java code." –Yishai Nov 1 '09 at 18:43 I concur. My answer was only addressing how to get the
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta java arithmeticexception Discuss the workings and policies of this site About Us Learn
Try Catch Block For Divide By Zero
more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us can you divide by zero in java Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, http://stackoverflow.com/questions/1657887/how-should-i-throw-a-divide-by-zero-exception-in-java-without-actually-dividing 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 a look: public class NewClass { public static void main(String[] args) { // TODO code application logic here double tab[] http://stackoverflow.com/questions/14137989/java-division-by-zero-doesnt-throw-an-arithmeticexception-why = {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,611103483 asked Jan 3 '13 at 11:25 Katie 73871836 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. –g
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 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 zero (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.