Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6851851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:17:53+00:00 2026-05-27T01:17:53+00:00

PHP defines two SPL exceptions for invalid keys: OutOfRangeException : Exception thrown when an

  • 0

PHP defines two SPL exceptions for invalid keys:

OutOfRangeException: Exception thrown when an illegal index was requested. This represents errors that should be detected at compile time.

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

As PHP isn’t a compiled language the distinction between compile-time and run-time seems strange and thus I am finding it hard to understand which exception to use when.

Currently my understanding is that one should throw…
… OutOfRangeException if the key is fundamentally and inherently malformed, e.g. if an array is passed as a key.
… OutOfBoundsException if the key is generally okay, but isn’t in some boundaries, e.g. if 100 is passed but 50 is the maximum key.

Is that understanding correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T01:17:54+00:00Added an answer on May 27, 2026 at 1:17 am

    While PHP doesn’t have a classic "compile time" (or a compiler that do a lot of static checks for that matter) I’d treat "compile time" as "rather static stuff I did wrong when writing the code" and "run time" as "my logic, input or validation was off at some point".

    So my suggestion would be to treat it like this:

    "Compile Time" / "OutOfRangeException": The error can always be fixed in the source code without or with very little logic.

    I always take numbers from 1-10 and you put in 11


    "Run Time" / "OutOfBoundsException": The error is due to wrong use at runtime.

    You created me and told me to take values from 1 to 5 then you put in 7. Doesn’t compute

    or

    You request an index that is not there because you didn’t put it there like you should


    Sample:

    I’d expect an SplFixedArray to throw an OutOfBoundsException because it’s size is dynamic and can chance at runtime while I’d expect something like a Calender::getMonthName to throw and OutOfRangeException because the number of month are definitely fixed at "compile/write" time.

    Array object sample:

    Say $array is an object that implements ArrayAccess you could throw an OutOfBoundsException in these circumstances:

    $array['bar'];
    $array[7];
    

    As the values are what you could expect for ArrayAccess but it doesn’t make sense in the case of an SplFixedArray(5). Alternatives would be DomainException or maybe RangeException

    An OutOfRangeException in these cases:

    $calendar->getMonth(15);
    

    As putting an array or a new class there is definitely some bigger logic flaw in the code that usually results from a simple "oh, i put in the wrong variable" error by a programmer. An (maybe preferable) alternative would be UnexpectedValueException and good old InvalidArgumentException.

    For cases like:

    $array[array()];
    $array[new StdClass];
    

    some of the alternative exceptions seem more fitting.

    Comparisons with the Java world on which exception to use when are not always applicable as Java Developers have an additions issue to deal with.

    Checked/Unchecked exceptions. With many people arguing that everything that isn’t a runtime exception has very limited use in Java / should not be used much internally) those names have lost some of their original meaning and intent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm including a file init.php which defines path constants. So if I include init.php
it's all in index.php /* Define site root */ defined('DOCUMENT_ROOT') ? null : define('DOCUMENT_ROOT',realpath(dirname(__FILE__)));
In core.php I can define Configure::write('Routing.admin', 'admin'); and /admin/controller/index will work. but if I
I want to define something like this in php : $EL = \n<br />\n;
Imagine I have this constant in PHP: define('APP_PATH', str_replace('//', '/', str_replace('\\', '/', dirname(__FILE__) .
NOTE: This question has been asked on the kohana forums at: http://forum.kohanaframework.org/comments.php?DiscussionID=6451 Hey everyone!
I have two associative arrays in PHP that are defined as following: $this_week[] =
I'm comparing two strings like so: <?php $Str1 = '111122223333444455556666'; $Str2 = '111122223333444455557777'; if($Str1
This is a very simple example: I have a table with two columns, id
I've been struggling with this for two days right now. At the moment I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.