Класс OutOfBoundsException

(PHP 5 >= 5.1.0, PHP 7)


Создается исключение, если значение не является действительным ключом. Это соответствует ошибкам, которые не могут быть обнаружены во время компиляции.

Обзор классов

OutOfBoundsException extends RuntimeException {
/* Наследуемые свойства */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Наследуемые методы */
final public string Exception::getMessage ( void )
final public Exception Exception::getPrevious ( void )
final public mixed Exception::getCode ( void )
final public string Exception::getFile ( void )
final public int Exception::getLine ( void )
final public array Exception::getTrace ( void )
final public string Exception::getTraceAsString ( void )
public string Exception::__toString ( void )
final private void Exception::__clone ( void )


i wish i know what's the difference between 
OutOfRangeException and OutOfBoundsException lol
2014-11-10 05:29:07
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.
2015-08-11 11:49:08
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]..

    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);

    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/>");
2016-07-04 13:48:33
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){
    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!';
2017-01-26 14:58:59
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):

class HandleApplication {
    public function 
__construct($_POST) {
            throw new 
OutOfBoundsException('Application hasn't sent secret code for authenticate);
2017-05-17 02:35:28

    Поддержать сайт на родительском проекте КГБ