8

What's the difference between TypeError and InvalidArgumentException in PHP 7?
When to throw TypeError and when to throw InvalidArgumentException?

It seems that the error is getting more like exception in PHP 7.
What's the border line that divides error and exception?

Terry Djony
  • 1,975
  • 4
  • 23
  • 41
  • for diff between Error and Exception, see also [this answer](https://stackoverflow.com/a/841528/2286722) and also comments – Marten Koetsier May 25 '17 at 14:56
  • @MartenKoetsier, that thread is more about the old errors (which cannot be caught) VS structured exception handling (classes implementing `Throwable`). – XedinUnknown Oct 01 '18 at 15:00

3 Answers3

14

TypeError occurs when the type of argument or returned value of a function doesn't match the expected type, for the example, if a function required a string but you passed/returned int.

InvalidArgumentException can be used to throw error if the argument doesn't match the expected value, for example if you want the argument only has value in range 1 to 10, but the caller pass the value 11, you can throw this exception.

Izhari Ishak Aksa
  • 858
  • 10
  • 13
  • This is the best answer as it points out the defining line between the two: is the syntax incorrect (type) or the semantics (value)? – parttimeturtle Feb 02 '21 at 18:18
0

TypeError is what mean the condition occurs when:

  1. argument type being passed to a function does not match its corresponding declared parameter type.
  2. Return type doesn't match with declared function return type.
  3. invalid number of arguments are passed to a built-in PHP function.

InvalidArgumentException is the exception thrown if an argument is not of the expected type.

Errors can be handled at runtime by catching \Throwable.

dotancohen
  • 30,064
  • 36
  • 138
  • 197
ram vinoth
  • 472
  • 4
  • 14
  • Of course errors can be handled at runtime. Descendants of `Error` implement the `Throwable` interface, and therefore can be caught just like descendants of `Exception`. – XedinUnknown Oct 01 '18 at 14:55
  • 2
    Errors can be handled since php7 – Kim Nov 12 '18 at 19:02
-3

InvalidArgumentException (source: http://php.net/manual/en/class.invalidargumentexception.php)

Exception thrown if an argument is not of the expected type.

Example (source: http://php.net/manual/en/class.invalidargumentexception.php)

function tripleInteger($int)
{
  if(!is_int($int))
    throw new InvalidArgumentException('tripleInteger function only accepts integers. Input was: '.$int);
  return $int * 3;
}

TypeError (source: http://php.net/manual/en/class.typeerror.php)

There are three scenarios where a TypeError may be thrown. The first is where the argument type being passed to a function does not match its corresponding declared parameter type. The second is where a value being returned from a function does not match the declared function return type. The third is where an invalid number of arguments are passed to a built-in PHP function (strict mode only).

louisfischer
  • 1,968
  • 2
  • 20
  • 38
Md. Abutaleb
  • 1,590
  • 1
  • 14
  • 24
  • 1
    Yes, we know how to read the PHP.net manual. This does not answer the question, which was: "When to throw TypeError and when to throw InvalidArgumentException?" – XedinUnknown Oct 01 '18 at 14:56
  • Exception thrown if an argument is not of the expected type. on the other hand, three scenarios where a TypeError may be thrown. Read the answer carefully then comment here. My answer in the two separate paragraph with InvalidArgumentException vs TypeError . Please read it carefully. I don't tell you to read php manual. I just attached a reference link. Thanks. – Md. Abutaleb Oct 01 '18 at 15:21
  • 1
    Yes, I see that. And you are right, `TypeError` is thrown in 3 different scenarios. 1 of those scenarios is the same as the one in which `InvalidArgumentException` is thrown. Hence the question: which one should we throw? Your answer does not answer that question. I mentioned the manual because you simply included the descriptions of the 2 types - from the manual. – XedinUnknown Oct 08 '18 at 15:01