Error Divide By Zero Java
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings java divide by zero exception and policies of this site About Us Learn more about Stack Overflow divide by zero error encountered the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags java divide by zero exception handling Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only java divide by zero try catch 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 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
Java Divide By Zero Float
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 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 51k24114226 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 d
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 Users double divide by zero java Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping error divide by zero excel 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: http://stackoverflow.com/questions/1657887/how-should-i-throw-a-divide-by-zero-exception-in-java-without-actually-dividing 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,566103482 asked Jan 3 '13 at 11:25 Katie 73371836 http://stackoverflow.com/questions/14137989/java-division-by-zero-doesnt-throw-an-arithmeticexception-why 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,416622 3 You need to check for Double.isNaN() as well. –Peter Lawrey Jan 3 '13 at 12:34 add a comment| up vote 42 down vote IEEE 754 defines
Method java.lang Class ArithmeticException java.lang.Object java.lang.Throwable java.lang.Exception java.lang.RuntimeException java.lang.ArithmeticException All Implemented http://www.homeandlearn.co.uk/java/java_error_handling.html Interfaces: Serializable public class ArithmeticException extends RuntimeException Thrown when an exceptional arithmetic condition has divide by occurred. For example, an integer "divide by zero" throws an instance of this class. ArithmeticException objects may be constructed by the virtual machine as if suppression were disabled divide by zero and/or the stack trace was not writable. Since: JDK1.0 See Also:Serialized Form Constructor Summary Constructors Constructor and Description ArithmeticException() Constructs an ArithmeticException with no detail message. ArithmeticException(Strings) Constructs an ArithmeticException with the specified detail message. Method Summary Methods inherited from classjava.lang.Throwable addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString Methods inherited from classjava.lang.Object clone, equals, <
can be broken up into two categories: Design-time errors and Logical errors. Design-time errors are easy to spot because NetBeans usually underlines them. If the error will prevent the program from running, NetBeans will underline it in red. Logical errors are the ones that you make as a programmer. The program will run but, because you did something wrong with the coding, there's a good chance the entire thing will crash. You'll see examples of run-time errors shortly. You'll also see how to handle them. But first a word on how Java handles errors. Exceptions In Java, errors are handled by an Exception object. Exceptions are said to be thrown, and it's your job to catch them. You can do this with a try catch block. The try catch block looks like this: try { } catch ( ExceptionType error_variable ) { } The try part of the try catch block means "try this code". If something goes wrong, Java will jump to the catch block. It checks what you have between the round brackets to see if you have handled the error. If you have the correct Exception type then whatever code you have between the curly brackets of catch will get executed. If you don't have the correct Exception type then Java will use its default exception handler to display an error message. As an example, create a new console application. Call it anything you like. In the code for the Main method, enter the following: try { int x = 10; int y = 0; int z = x / y; System.out.println( z ); } catch ( Exception err ) { System.out.println( err.getMessage( ) ); } In the try part of the try catch block, we have set up three integers, x, y and z. We are trying to divide y into x, and then print out the answer. If anything goes wrong, we have a catch part. In between the round brackets of