Bug #69957 [NEW]: Three different ways of handling same error condition
| From: | cameron dot adam+php at gmail dot com | Date: | Sun, 28 Jun 2015 14:24:02 +0000 |
| Subject: | Bug #69957 [NEW]: Three different ways of handling same error condition | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-193960@lists.php.net to get a copy of this message | ||
From: cameron dot adam+php at gmail dot com
Operating system: Windows 7 64-bit
PHP version: 7.0.0alpha2
Package: Scripting Engine problem
Bug Type: Bug
Bug description:Three different ways of handling same error condition
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 bug report at https://bugs.php.net/bug.php?id=69957&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=69957&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=69957&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=69957&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=69957&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=69957&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=69957&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=69957&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=69957&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=69957&r=support
Expected behavior: https://bugs.php.net/fix.php?id=69957&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=69957&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=69957&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=69957&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=69957&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=69957&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=69957&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=69957&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=69957&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=69957&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=69957&r=mysqlcfg