Php Catch Division By Zero 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 php division by zero exception Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs how to solve division by zero in php Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, php ignore division by zero just like you, helping each other. Join them; it only takes a minute: Sign up Try/Catch block in PHP not catching Exception up vote 44 down vote favorite 7 I am trying to run this Example #1
Warning Division By Zero In Wordpress
from this page: http://php.net/manual/en/language.exceptions.php getMessage(), "\n"; } // Continue execution echo "Hello World\n"; ?> However instead of the desired output I get: 0.2 Fatal error: Uncaught exception 'Exception' with message 'Division by zero.' in xxx: 7 Stack trace: #0 php catch all exceptions xxx(14): inverse(0) #1 {main} thrown in xxx on line 7 The developer environment I am using is UniServer 3.5 with PHP 5.2.3 php share|improve this question edited Mar 13 '14 at 19:14 Eric Leschinski 47.1k23221191 asked Jan 31 '10 at 18:09 s7orm 82111121 1 Can you show us your code? The only mistake you can do to get this error is catching the wrong exception (or none). –Tammo Jan 31 '10 at 18:13 Code is EXACTLY identical (I have just added some newlines)... anyway, I copied the code once more into a test file and here is the same error messsage: 0.2 Fatal error: Uncaught exception 'Exception' with message 'Division by zero.' in W:\www\test.php:4 Stack trace: #0 W:\www\test.php(11): inverse(0) #1 {main} thrown in W:\www\test.php on line 4 I have really no idea whats going on there... maybe faulty PHP configuration? –s7orm Jan 31 '10 at 18:25 Some older extension versions caused problems with exception handling. 5.2.3 is old and a bug may be behind the error. Can you upgrade PHP? UniServer 3.5 is also quite old, considering the current production version is 5.5. Is 3.5 a typo? –outis Jan 31 '10 at 23:39 Looking at the UniServer release info (wiki.uniformserver.com/index.php/…), 3.5 apparently isn't a typo. Upgrade to UniServer 5.5 and try the sam
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 php catch warning Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation
Php Suppress Warnings
Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like
Php Exceptions
you, helping each other. Join them; it only takes a minute: Sign up Division by zero, php up vote 0 down vote favorite I have created this function that correctly displays the infinity symbol when trying to enter http://stackoverflow.com/questions/2172715/try-catch-block-in-php-not-catching-exception 0 into the Y text box. However I still get the division by zero error... here is my code. here is the case for switch statment case '/': $prod = $x / $y; break; hh //check for divide by 0 function check0($y) { if ($y == 0) throw new Exception ($prod = "∞"); return FALSE; } try { check0($y); $prod = $x /$y; }catch(Exception $zero){ echo $zero->getMessage(); } php divide-by-zero share|improve this question edited Oct http://stackoverflow.com/questions/7904118/division-by-zero-php 26 '11 at 15:06 Austin Salonen 33.5k981113 asked Oct 26 '11 at 14:27 Marc Howard 315 how is the code actually organized? I see a divide and then a break (no call to check0 at the top. –Evan Teran Oct 26 '11 at 14:30 What's this line $prod = $x / $y; before the break? Is it surrounded by a try{...}catch{...} ? –Aurelio De Rosa Oct 26 '11 at 14:30 2 The current answer covers it, but some additional notes: why is there an assignment inside the Exception constructor? Also, you shouldn't be using an exception for control flow. Just use an if statement to return the infinity sign instead of doing the division. –Tesserex Oct 26 '11 at 14:32 can you post the error that you get? there should be a line number in the error which should give you a hint what you have wrong –Martin Taleski Oct 26 '11 at 14:33 What if $x is negative? Negative infinity is a possibility too... –Marc B Oct 26 '11 at 14:43 add a comment| 5 Answers 5 active oldest votes up vote 2 down vote First: you are doing a division at the second line code (which can be devision by zero). Second: no need to return false in your method since you are thro
your question and get tips & solutions from a community of 418,617 IT Pros & Developers. It's quick & easy. My try-catch doesn' t "catch". What's wrong? P: n/a lwoods Why isn't my https://bytes.com/topic/php/answers/157369-my-try-catch-doesn-t-catch-whats-wrong "catch" catching? TIA, Larry Woods This script get's this output:: Warning: Division by zero in C:\Inetpub\wwwroot\PHP\tests.php on line 4 This will never be printed Script: ( 5.0.4; XP Pro; IIS ) http://programmers.stackexchange.com/questions/208677/how-to-handle-divide-by-zero-in-a-language-that-doesnt-support-exceptions 0; // simple error - division by zero print "This will never be printed"; } catch (Exception $e) { print "Exception caught:
"; print "Code: ".$e->getCode()."
"; print "Message: ".$e->getMessage()."
"; print "Error thrown on Line: ".$e->getLine()."
"; print "Trace: division by
".$e->getTraceAsString."
"; } ?> Aug 7 '05 #1 Post Reply Share this Question 3 Replies P: n/a Janwillem Borleffs lwoods wrote: Why isn't my "catch" catching? With try-catch only exceptions which are actually thrown will be handled, not warnings: class Math { function divide($a, $b) { if ($b == 0) { throw new Exception('division by zero'); } else { return $a / $b; } } } try { $math = new Math; print $math->divide(2, 0); division by zero }catch (Exception $e) { print "Exception caught:
"; print "Code: ".$e->getCode()."
"; print "Message: ".$e->getMessage()."
"; print "Error thrown on Line: ".$e->getLine()."
"; } JW Aug 7 '05 #2 P: n/a lwoods Thanks.... Larry "Janwillem Borleffs"
"; print "Code: ".$e->getCode()."
"; print "Message: ".$e->getMessage()."
"; print "Error thrown on Line: ".$e->getLine()."
"; } JW Aug 7 '05 #3 P: n/a Wayne ....But you can throw exceptions from the error handler (see set_error_handler()) and this will work correctly. So you could throw a generic exception from the error handler which would get called on divide by zero and then be caught by your exception handler. Aug 8 '05 #4 This discussion thread is closed Start new discussion Replies have been disabled for this discussion. Similar topics whats wrong with my code? Can someone examine this link and tell me whats wrong? Whats wrong with this SqlParameter ? whats wrong with this? PHP and dates? Whats wrong with timestamp and julian callend
tour help Tour Start 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 posting ads with us Software Engineering Questions Tags Users Badges Unanswered Ask Question _ Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle who care about creating, delivering, and maintaining software responsibly. 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 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 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