Re: Which Exception to use ....
| From: | Jochem Maas | Date: | Sat, 23 Aug 2008 12:30:29 +0000 |
| Subject: | Re: Which Exception to use .... | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-278562@lists.php.net to get a copy of this message | ||
David Otton schreef:
2008/8/22 Jochem Maas <jochem@iamjochem.com>:aha. that clears up the definition of 'Domain' for me, thank you.still leaves the questions as to what a DomainException and a ErrorException is meant to model.A domain exception is thrown when a value is valid according to its type, but not within the domain of the function that it is being passed to. For example if you had a function that only processed odd numbers, passing it (int)6 would be a domain exception.
ErrorExceptions model genuine PHP errors - the mechanism that you manipulate with error_reporting(). You can convert a PHP error to an Exception with set_error_handler().I guessed as much, but wasn't sure ... the manual should document this as best practice me thinks ... I have various projects that convert errors to exceptions but I always used a custom PhpErrorException :-)
Your list brings together exceptions from all over PHP which shouldn't really be considered together.true, it was merely a dump from the following oneliner: php -r 'foreach(get_declared_classes() as $c) if (stripos($c, "Exception") !== false) echo "$c\n";'
For example, many of them are from the SPL. They're supposed to be generic - that's what the SPL is for, it's there to solve a specific class of problems.I figured that there could/should be a few more that cover more general web application errors. but it's probably very difficult to come up with a set that is adequate ... in which case it's no wonder that SPL only covers subclassed Exceptions for very specific situations
In that context, those exceptions make complete sense. Other exceptions on your list are only ever meant to be thrown from within PHP itself - they're there for us to catch, not to throw.5 of them to be exact, those were clear to me.
Take a look at the Zend Framework as an example of best practice in these things. I don't think that throws a single built-in exception (although some of the SPL exceptions might be nice to use, they mostly solve the same class of problems as assert()).can't say I've ever used assert() although I know of it. I'll take another dive into ZF soonish to see what they're upto with regard to Exceptions. thanks for the feedback :-)