Re: Req #67860 [NEW]: Throw an "acceptable" exception
| From: | Martin Scotta | Date: | Tue, 19 Aug 2014 02:39:04 +0000 |
| Subject: | Re: Req #67860 [NEW]: Throw an "acceptable" exception | ||
| References: | 1 | Groups: | php.standards |
| Request: | Send a blank email to standards-+get-399@lists.php.net to get a copy of this message | ||
I love this one!
How about a marker interface instead of a new keyword?
class DivisionByZero extends Exception implements CasualException ...
El ago 18, 2014 10:13 PM, "david dot proweb at gmail dot com" <
php-bugs@lists.php.net> escribió:
> From: david dot proweb at gmail dot com
> Operating system: Irrelevant
> PHP version: Irrelevant
> Package: PHP Language Specification
> Bug Type: Feature/Change Request
> Bug description:Throw an "acceptable" exception
>
> 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 bug report at
> https://bugs.php.net/bug.php?id=67860&edit=1
> --
>
>
> --
> PHP Naming Standardization Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>