Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 750
Terry Djony Avatar asked May 24 '17 04:05

Terry Djony


1 Answers

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.

like image 151
Izhari Ishak Aksa Avatar answered Nov 16 '22 02:11

Izhari Ishak Aksa