Bug #69957 [Csd]: Three different ways of handling same error condition
| From: | cameron dot adam+php at gmail dot com | Date: | Sun, 28 Jun 2015 16:41:33 +0000 |
| Subject: | Bug #69957 [Csd]: Three different ways of handling same error condition | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-193968@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
User updated by: cameron dot adam+php at gmail dot com
Reported by: cameron dot adam+php at gmail dot com
Summary: Three different ways of handling same error
condition
Status: Closed
Type: Bug
Package: Scripting Engine problem
Operating System: Windows 7 64-bit
PHP Version: 7.0.0alpha2
Assigned To: bwoebi
Block user comment: N
Private report: N
New Comment:
OK, just to be clear... a *catchable* error, yes?
Thanks for jumping on this so quickly, btw :-)
--
Adam
Previous Comments:
------------------------------------------------------------------------
[2015-06-28 16:25:32] bwoebi@php.net
Note that it now all is an Error. We may eventually change it to a more specific error, but Error is
good enough for the moment, I think.
See http://git.php.net/?p=php-src.git;a=blob;f=Zend/tests/bug69957.phpt;h=d6f441b6ea93ae50607ddbeb118aca32361691f3;hb=fb08798c9f0ea820d567668d0cea4833dc61dd8e
for current behavior.
------------------------------------------------------------------------
[2015-06-28 16:23:03] bwoebi@php.net
Automatic comment on behalf of bobwei9@hotmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=fb08798c9f0ea820d567668d0cea4833dc61dd8e
Log: Fix bug #69957 (Different ways of handling div/mod by zero)
------------------------------------------------------------------------
[2015-06-28 16:07:16] bwoebi@php.net
Okay, I see we should normalize div and mod⦠;-)
------------------------------------------------------------------------
[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