rfc:spl-improvements:exceptions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:spl-improvements:exceptions [2012/02/24 22:54] – [OutOfRange and OutOfBounds] levimrfc:spl-improvements:exceptions [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 12: Line 12:
 ===== Problems ===== ===== Problems =====
  
-==== General Documentation Issues ====+==== UnderflowException and OverflowException ====
  
-  * There are no examples that show when to throw the specific exception[[http://www.php.net/manual/en/spl.exceptions.php#102226|Dawid Krysiak lamented this in a comment]]. +By name, people often think of underflow and overflow as mathematical overflows; you did some addition and overflowed the size of an integer, for exampleHowever, their definitions and usage are quite different.
-  * Descriptions are too short. +
-==== Logic and Runtime Exceptions ====+
  
-=== What is a LogicException? ===+UnderflowException is really defined as an exception that occurs when you call method on an object that can not be used because the object is 'empty'. Example: calling pop on an empty stack.
  
-A LogicException is currently documented as+OverflowException is really defined as an exception that occurs when you call a method on an object that can not be used because the object is 'full'Example: attempting to add another index in an SplFixedArray.
-"Exception that represents error in the program logicThis kind of exceptions should directly lead to a fix in your code."+
  
-**Known subclasses:** +I propose that we create three new exceptions:
-  * [[http://www.php.net/manual/en/class.badfunctioncallexception.php|BadFunctionCallException]] +
-  * [[http://www.php.net/manual/en/class.domainexception.php|DomainException]] +
-  * [[http://www.php.net/manual/en/class.invalidargumentexception.php|InvalidArgumentException]] +
-  * [[http://www.php.net/manual/en/class.lengthexception.php|LengthException]] +
-  * [[http://www.php.net/manual/en/class.outofrangeexception.php|OutOfRangeException]]+
  
 +  * StateException extends RuntimeException
 +    * EmptyException extends StateException
 +    * FullException extends StateException
  
 +This provides a general state exception for users to use (this has been requested). It additionally provides the semantic meaning of the current OverflowException and UnderflowException. We would then have OverflowException be an alias of FullException and UnderflowException be an alias of EmptyException. We may consider deprecating Overflow and Underflow exceptions. These measures are fully BC (unless there are bugs with aliases and exceptions, but simple tests I conducted showed expected behavior).
  
-=== What is a RuntimeEexception? ===+===== Patches =====
  
-RuntimeException is currently documented as: +WIP for the state exceptionshttps://github.com/morrisonlevi/php-src/tree/StateExceptions
-"Exception thrown if an error which can only be found on runtime occurs." +
- +
-**Known subclasses:** +
-  * [[http://www.php.net/manual/en/class.outofboundsexception.php|OutOfBoundsException]] +
-  * [[http://www.php.net/manual/en/class.overflowexception.php|OverflowException]] +
-  * [[http://www.php.net/manual/en/class.rangeexception.php|RangeException]] +
-  * [[http://www.php.net/manual/en/class.underflowexception.php|UnderflowException]] +
-  * [[http://www.php.net/manual/en/class.unexpectedvalueexception.php|UnexpectedValueException]] +
-==== OutOfRange and OutOfBounds ==== +
- +
-The name "OutOfRangeException" is misleading.  It is mainly used within to the SPL to denote that you have given an incorrect index type. Its name implies that something is out of a defined range, such as the size of an array.  That is what OutOfBoundsException was created for.   +
- +
-Consider the situation when you are overloading ArrayAccess to only allow strings for offsets.  When a non-string offset is provide, what exception should be thrown?  InvalidArgumentException doesn't make sense because you didn't seeming make a function call.  The name OutOfRangeException implies that you specified an unknown value of the correct type, and so does OutOfBoundsException. +
-==== Domain and InvalidArgument ==== +
- +
-It is not always clear when each should be used. Consider the following situation where we define a function that does integer division: +
-<code php> +
-function intDivide($dividend, $divisor) { +
-    //add error handling +
- +
-    return $dividend / $divisor; +
-}  +
- +
-intDivide(1, new StdClass()); // incorrect type +
-intDivide(1, 0); //correct type, incorrect value +
-</code> +
-The first error has to deal with incorrect type, and the second is a correct type but the value is not valid for division. These are clearly different errors. Which is a DomainException and which is an InvalidArgumentException? Are they both DomainExceptions? InvalidArgumentExceptions? +
-===== Proposed Solutions ===== +
- +
-==== Provide Inheritance Hierarchies ==== +
- +
-In the [[http://www.php.net/manual/en/spl.exceptions.php|overview for SPL exceptions]], the table of contents lists all of the exceptions defined in the SPL. This should be changed to list the exception hierarchy: +
- +
-  Exception +
-    LogicException +
-      BadFunctionCallException +
-        BadMethodCallException +
-      DomainException +
-      InvalidArgumentException +
-      LengthException +
-      OutOfRangeException +
-    RuntimeException +
-      OutOfBoundsException +
-      OverflowException +
-      RangeException +
-      UnderflowException +
-      UnexpectedValueException +
-       +
-Fortunately, [[http://us3.php.net/manual/en/spl.exceptions.php#102226|Dawid Krysiak]] submitted it as a comment on 02 Feb 2011.  It would be helpful to also list the subclasses of each exception on their individual pages. +
-==== Provide Examples ==== +
- +
-Every exception needs an example of how to use it, giving preference to examples taken from the SPL data-structures.  This will also increase the awareness of the SPL data-structures and where they throw exceptions.+
rfc/spl-improvements/exceptions.1330124040.txt.gz · Last modified: 2017/09/22 13:28 (external edit)