The OutOfBoundsException class

(PHP 5 >= 5.1.0, PHP 7)

简介

Exception thrown if a value is not a valid key. This represents errors that cannot be detected at compile time.

类摘要

OutOfBoundsException extends RuntimeException {
/* 继承的属性 */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* 继承的方法 */
final public Exception::getMessage ( void ) : string
final public Exception::getPrevious ( void ) : Throwable
final public Exception::getCode ( void ) : int
final public Exception::getFile ( void ) : string
final public Exception::getLine ( void ) : int
final public Exception::getTrace ( void ) : array
final public Exception::getTraceAsString ( void ) : string
public Exception::__toString ( void ) : string
final private Exception::__clone ( void ) : void
}

User Contributed Notes

Jakub Adamczyk 16-May-2017 11:35
I see this in this way:
By definiton, OutOfRangeException should be use to when potential problem is logical. This (OutOfBoundsException) is for runtime, so it's perfect for catching errors which occur because of bad result from database and simillar.

Example of using OutOfBoundSException (see also note in "OutOfRangeException class" article):

<?php
class HandleApplication {
    public function
__construct($_POST) {
        if(!isset(
$_POST['secretCode'])
            throw new
OutOfBoundsException('Application hasn't sent secret code for authenticate');
}
evguenia dot chagnon at gmail dot com 26-Jan-2017 12:58
class MyDynamicTastyPie implements ArrayAccess{
    private $_pointeur = 0;
    private $_array = ['strawberry slice','white chocolate','nuts'];
   
    public function offsetExists($key){
        return isset($this->_array[$key]);
    }
   
    public function offsetGet($key){
        if ($key > count($this->_array)){
            throw new OutOfBoundsException('Your tasty pie doesn\'t contain so slices');
        }
        return $this->_array[$key];
    }
   
    public function offsetSet($key, $value){
        $this->_array[$key] =$value;
    }
   
    public function offsetUnset($key){
        unset($this->_array[$key]);
    }
   
    public function addSlice($slice){
        $this->_array[] = $slice;
    }
}

try {
    $myDynamicTastyPie = new MyDynamicTastyPie();
    $myDynamicTastyPie->addSlice('Black chocolate cream decoration');
    echo $myDynamicTastyPie[7];
}
catch(OutOfBoundsException $e){
    echo 'Here is your OutOfBoundsException!';
}
sricharan dot krishnan at gmail dot com 04-Jul-2016 10:48
An example where an OutOfBoundsException can occur:
Lets say post a certain division process, we wish to access a value in an Array [provided ofcourse if the result value of the division is within the size of the Array]..

try{
    if ($iNum2 == 0){
        throw new Exception("Division by Zero");
    }
    $iResult = $iNum1 / $iNum2;
    echo ("Division result is: ".($iResult)."<br/>");
}
catch (Exception $e){
    echo ("Division by Zero is not possible.".($e)."<br/>");
}

$rg_Array = array(1,2,3,4);

try{
    if ($iResult > sizeof($rg_Array)- 1){
        throw new Exception("Exceeding key values");
    }
echo ("Capturing value from \$rg_Array post Division process:".($rg_Array[$iResult])."<br/>");
}
catch (Exception $e){
    echo ("Value of Division result is out of bounds for the array.".($e)."<br/>");
}
?>
Jacob V. Rasmussen 11-Aug-2015 08:49
OutOfRangeException is for Integers out of range.
OutOfBoundsException is for key values, not found in the target array.

Editor's note: This is incorrect; OutOfRangeException has *nothing* to do with Integer ranges. I decided to keep this highly voted comment even though it is wrong for education's sake.
Anonymous 10-Nov-2014 03:29
i wish i know what's the difference between
OutOfRangeException and OutOfBoundsException lol