Req #67860 [Opn]: Throw an "acceptable" exception
| From: | david dot proweb at gmail dot com | Date: | Tue, 19 Aug 2014 01:56:07 +0000 |
| Subject: | Req #67860 [Opn]: Throw an "acceptable" exception | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-187173@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=67860&edit=1
ID: 67860
User updated by: david dot proweb at gmail dot com
Reported by: david dot proweb at gmail dot com
Summary: Throw an "acceptable" exception
Status: Open
Type: Feature/Change Request
Package: *General Issues
Operating System: Irrelevant
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Actually it would make errors in a casual exception, so that I could capture it in a more reliable
manner.
$variable = [1, 2, 3];
echo $variable;
// Notice: Array to string conversion.
How I can capture that? (In this case, I could avoid it by checking if the variable is compatible,
but it is only an example.)
With the feature, I could do:
try { echo $variable; }
catch(Exception $e) {
if(strpos($e->getMessage(), "Array to string conversion")) {
echo "Check option
user_type, it accept only string.";
}
}
Previous Comments:
------------------------------------------------------------------------
[2014-08-19 01:49:21] requinix@php.net
If you don't want to deal with exceptions then don't use them in your code.
------------------------------------------------------------------------
[2014-08-19 01:13:31] david dot proweb at gmail dot com
Description:
------------
Imagine that you do ($a / $b), but $b is zero.
So you will receives "Warning: Division by zero.".
You can avoid that by doing @($a / $b), something like that.
But imagine if PHP can thrown an exception that not need be captured.
It is: you can, optionally, capture that, but if not, will only thrown a notice, instead of warning.
// Supposing that it is the PHP division constructor:
function php_div($a, $b) {
if($b === 0) {
throw casual new Exception("Division by zero.");
}
return $a / $b;
}
// So if I do:
5 / 0; // Notice: Division by zero.
// But I can do:
try { 5 / 0; }
catch(Exception $e) { ... } // Avoid notice.
// Or even:
@(5 / 0); // Avoid casual Exception.
--
User can throw casual exceptions too.
For instance:
function name_fix($name) {
...
if(name_uncommon($name)) {
throw casual new Exception("This name is uncommon.");
}
...
return $name_fixed;
}
echo name_fix("David"); // David
echo name_fix("Hakuna Matata"); // Hakuna Matata + Notice
// When you capture that, statement will be blocked.
try { echo name_fix("Hakuna Matata"); }
catch(Exception $e) { echo "The Hakuna Matata is so uncommon."; }
// -> The Hakuna Matata is so uncommon. (not echo name, not notice)
--
Exception need a new property like boolean $isCasual ($e->isCasual()).
--
Note: apply it to "division by zero" is only to examplify my feature request.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67860&edit=1