0

Is there an appropriate standard PHP exception which can be used when an array is expected to contain an associative value, which is not correct?

For example, I have a method which takes an array of 'configuration' parameters. Those parameters are validated to check that the required ones are there and that they are of the correct type. If any of the required parameters are incorrect or missing, the exception is thrown.

I was wondering what the most appropriate standard exception would be for this, if one exists at all.

Note

  • The array key might be missing OR might be present but incorrect value.

  • The array values may be of any type.

I'm leaning towards UnexpectedValueException

2

3 Answers 3

0

How about OutOfBoundsException?

http://php.net/manual/en/class.outofboundsexception.php

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

Sign up to request clarification or add additional context in comments.

Comments

0

BadMethodCallException covers the invalid, or missing, arguments of a method call. This would make the most sense for your example.

Comments

0

I went with DomainException, which I believe is the most appropriate.

Exception thrown if a value does not adhere to a defined valid data domain.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.