On 7 October 2014 18:52:35 GMT+01:00, Levi Morrison <le...@php.net> wrote: >On Tue, Oct 7, 2014 at 7:58 AM, Rowan Collins <rowan.coll...@gmail.com> >wrote: >> As an example where a specific exception would be useful, but a >generic one >> would not, consider a fluent interface which sometimes bugs out and >returns >> NULL. If wrapped in a NullPointerException these expected error >conditions >> could be caught at runtime, while any other errors would continue to >have >> their normal behaviour (such as triggering a higher-scoped >catch-all/default >> exception handler). > >I would contend that catching a NullPointerException is not any more >recoverable than most other EngineExceptions.
In the scenario I just listed, the catch is effectively emulating an isset()/is_object() check before each chained operation. Seems perfectly reasonable to me. (If I'm not much mistaken, CoffeeScript has an operator for this precise case.) > I would also argue that >you should never, in any language, catch a NPE. The only time you >should catch an NPE is if you are catching all exceptions, such as in >a FastCGI daemon that will cleanly shutdown afterwards. If you're not allowed to catch it, why make it an exception? Benefits such as unwinding finally blocks and destructors, and collecting backtraces, are secondary effects, not really fundamental to exceptions. Perhaps "NullPointerException" is not a good name, as it has rather different implications than in other languages. In PHP, using the -> operator on a non-object isn't much different from calling a function with incorrect types. It shouldn't happen if your code is properly unit-tested, but it doesn't necessarily mean a catastrophic failure has occurred. Regards, -- Rowan Collins [IMSoP] -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php