How To Handle Divide By Zero 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 java divide by zero error this site About Us Learn more about Stack Overflow the company Business java divide by zero infinity Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask try catch divide by zero 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 takes a minute: Sign up divide by zero exception in c++ program try-catch for division by zero up vote 13 down vote favorite 1 I am a java newbie, my question is about try-catch blocks on a simple division by zero example. You see the first line of try? If I cast any of those two variables to the double the program does not recognize the catch block. In my opinion, whether I cast or
Divide By Zero Exception In C#
not only the catch block must be executed. What is wrong on this code? Thanks. public static void main(String[] args) { int pay=8,payda=0; try { double result=pay/(double)payda; // if I cast any of the two variables, program does not recognize the catch block, why is it so? System.out.println(result); System.out.println("inside-try"); } catch (Exception e) { System.out.println("division by zero exception"); System.out.println("inside-catch"); } } java try-catch divide-by-zero share|improve this question edited Oct 1 '12 at 9:46 Aziz Shaikh 11.6k73954 asked Feb 13 '10 at 21:50 Firat 66113 add a comment| 7 Answers 7 active oldest votes up vote 22 down vote Divide by zero is valid for floating point numbers. 1/0 yields Infinity. (-1)/0 yields -Infinity. 0/0 yields NaN. These "numbers" are properly defined in IEEE 754. Integer division by zero, on the other hand, throws because one cannot represent infinity as an int. share|improve this answer answered Feb 13 '10 at 21:54 kennytm 318k62752812 If floating point division cannot throw an exception, then the try / catch should be removed and error codes used instead. –Hamish Grubijan Feb 14 '10 at 2:30 1 I don't
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies
Java Arithmeticexception
of this site About Us Learn more about Stack Overflow the company divide by zero exception in oracle Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges divide by zero exception excel 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 takes a minute: http://stackoverflow.com/questions/2259190/try-catch-for-division-by-zero Sign up How does Java handle division by zero? [duplicate] up vote 16 down vote favorite 1 This question already has an answer here: In java, “5/0” statement doesn't fire SIGFPE signal on my Linux machine, why? 6 answers Does it simply check if divisor is different from zero every time there is division done (even in JIT-ed code)? I mean how http://stackoverflow.com/questions/21269461/how-does-java-handle-division-by-zero VM manages to throw an exception without being previously killed by the OS? java divide-by-zero share|improve this question asked Jan 21 '14 at 21:27 mrpyo 1,01311330 marked as duplicate by Raedwald, Eric Leschinski, Kevin Panko, Lego Stormtroopr, iandotkelly Jan 22 '14 at 2:43 This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. What do you mean by "previously killed"? –Oliver Charlesworth Jan 21 '14 at 21:29 One word: Zombies!! –Hot Licks Jan 21 '14 at 21:29 1 The OS won't kill the VM for a divide-by-zero. The VM will terminate if such an exception is not catched. –Stefano Sanfilippo Jan 21 '14 at 21:30 1 Division by zero is caught at hardware level and results in interrupt being called with usually leads OS to stopping the process (I'm asking how it is caught internally in VM implementation not language itself)... –mrpyo Jan 21 '14 at 21:32 2 @StefanoSanfilippo yeah the OS doesn't kill anything but the VM commits suicide ;-)
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 more about hiring developers or http://programmers.stackexchange.com/questions/208677/how-to-handle-divide-by-zero-in-a-language-that-doesnt-support-exceptions posting ads with us Programmers Questions Tags Users Badges Unanswered Ask Question _ Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute: Sign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top How to handle divide by zero in a language that doesn't support exceptions? up vote divide by 58 down vote favorite 8 I'm in the middle of developing a new programming language to solve some business requirements, and this language is targeted at novice users. So there is no support for exception handling in the language, and I wouldn't expect them to use it even if I added it. I've reached the point where I have to implement the divide operator, and I'm wondering how to best handle a divide by zero error? I seem to have divide by zero only three possible ways to handle this case. Ignore the error and produce 0 as the result. Logging a warning if possible. Add NaN as a possible value for numbers, but that raises questions about how to handle NaN values in other areas of the language. Terminate the execution of the program and report to the user a severe error occurred. Option #1 seems the only reasonable solution. Option #3 is not practical as this language will be used to run logic as a nightly cron. What are my alternatives to handling a divide by zero error, and what are the risks with going with option #1. programming-languages error-handling share|improve this question asked Aug 18 '13 at 11:45 ThinkingMedia 10.1k33366 12 if you did add exception support and the user didn't catch it then you'd have option #3 –ratchet freak Aug 18 '13 at 11:54 82 I'm curious, what kind of stupid requirement would require you to create a whole new programming language? In my experience, every language ever created sucks (in design or in execution, often in both) and it took unreasonably much effort to even get that much. There are a few exceptions to the first, but not to the second, and as they're easily <0.01% of the cases, they're probably measurement errors ;-) –delnan Aug 18 '13 at 12:04 16 @delnan most new languages are created to allow business rules to b