Bug #69957 [Opn->Asn]: Three different ways of handling same error condition

From: Date: Sun, 28 Jun 2015 16:07:17 +0000
Subject: Bug #69957 [Opn->Asn]: Three different ways of handling same error condition
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-193964@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69957&edit=1 ID: 69957 Updated by: bwoebi@php.net Reported by: cameron dot adam+php at gmail dot com Summary: Three different ways of handling same error condition -Status: Open +Status: Assigned Type: Bug Package: Scripting Engine problem Operating System: Windows 7 64-bit PHP Version: 7.0.0alpha2 -Assigned To: +Assigned To: bwoebi Block user comment: N Private report: N New Comment: Okay, I see we should normalize div and mod… ;-) Previous Comments: ------------------------------------------------------------------------ [2015-06-28 14:24:01] cameron dot adam+php at gmail dot com Description: ------------ I've included three files below (originals at https://github.com/adamcameron/php/tree/master/php.local/www/experiment/compilation/literal) There are six runtime variations in all, testing divide-by-zero operations with each of / and then %. There are three different error variations depending on the test. There should be only one: a runtime DivisionByZeroException, or perhaps an ArithmeticException, or even just an ExpressionException or something. But only *one* treatment of all six variations. See http://blog.adamcameron.me/2015/06/php-7-three-different-ways-of-dealing.html for equiv code run in CFML and Ruby demonstrating they do not share PHP's error-handling "vagaries" here. Apologies for uncharitable tone re PHP in that article, but I think it's mostly warranted. Cheers. Test script: --------------- <?php // literal.php $operator = $_GET['operator'] ?? 'divide'; try { if ($operator == 'divide'){ $result = 1 / 0; }else{ $result = 1 % 0; } } catch (Throwable $t){ echo "Operator: $operator<br>"; printf("Type: %s<br>", get_class($t)); printf("Message: %s<br>", $t->getMessage()); } <?php // literalViaVariable.php $operator = $_GET['operator'] ?? 'divide'; $divisor = 0; try { if ($operator == 'divide'){ $result = 1 / $divisor; }else{ $result = 1 % $divisor; } } catch (Throwable $t){ echo "Operator: $operator<br>"; printf("Type: %s<br>", get_class($t)); printf("Message: %s<br>", $t->getMessage()); } <?php // viaParam.php $operator = $_GET['operator'] ?? 'divide'; $divisor = $_GET['divisor'] ?? 1; try { if ($operator == 'divide'){ $result = 1 / $divisor; }else{ $result = 1 % $divisor; } } catch (Throwable $t){ echo "Operator: $operator<br>"; printf("Type: %s<br>", get_class($t)); printf("Message: %s<br>", $t->getMessage()); } Expected result: ---------------- The same catchable runtime exception to be thrown for all six possible variations of the three code examples above. Actual result: -------------- 1) Fatal error in the literal.php example 2) Warning when using / operator in runtime divide by zero situations 3) Throwable when using % operator in runtime divide by zero situations ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=69957&edit=1

« previous php.bugs (#193964) next »