Req #60341 [Opn]: SplFixedArray should throw specific exceptions.
Edit report at https://bugs.php.net/bug.php?id=60341&edit=1
ID: 60341
User updated by: morrison dot levi at gmail dot com
Reported by: morrison dot levi at gmail dot com
-Summary: SplFixedArray throws generic exceptions.
+Summary: SplFixedArray should throw specific exceptions.
Status: Open
Type: Feature/Change Request
Package: SPL related
Operating System: irrelevant
-PHP Version: Irrelevant
+PHP Version: 5.3
Block user comment: N
Private report: N
New Comment:
This should really be titled 'SplFixedArray should throw specific exceptions'
Previous Comments:
------------------------------------------------------------------------
[2011-11-19 21:31:32] morrison dot levi at gmail dot com
Description:
------------
SplFixedArray thankfully throws exceptions when you try to do incorrect things
with indices. However, the types of exceptions are just too generic. If I give
the wrong type of index, that's a logic error (I'd expect InvalidArgument or at
least something that inherits from LogicError to be thrown). If I give an index
that's that's a valid type but doesn't exist, I'd expect an OutOfBoundsException
to be thrown. Instead I get a generic RuntimeException.
I should expect because they are very different problems that I would at least
get a distinguishing message between the two. However, I get the same
descriptions: 'Index invalid or out of range'. The very message suggests they
should be different exceptions.
The first fix would sort-of break backwards compatibility: throw an
InvalidArgumentException for things of the wrong type.
The second fix, throw OutOfBoundsException on incorrect index, could be
implemented and keep backwards compatibility.
Test script:
---------------
$fa = new SplFixedArray(1);
$fa[new StdClass()]; //expect InvalidArgumentException or perhaps OutOfRangeException
$fa[2] = 'james'; // expect OutOfBoundsException
Expected result:
----------------
I expect $fa[new StdClass] to throw an InvalidArgumentException, not a
RuntimeException
I expect $fa['2'] to throw OutOfBoundsException not a RuntimeException.
Actual result:
--------------
Both throw RuntimeExceptions.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60341&edit=1
Thread (8 messages)