Search code examples
phpexceptionerror-handlingphp-7

TypeError vs InvalidArgumentException?


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?


Solution

  • 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.