7
In PHP, most native functions return a boolean false
, whole 0
or NULL
if some type of inconsistency is found in the value passed to it. Example:
$exemplo1 = explode("","tente me explodir"); // false
//ou
$exemplo2 = count(NULL) // 0
This type of solution ends up leaving the job of validating the function to the developer, who decides how (or if) to treat the same.
On the other hand, if all the functions went off exceptions
the developer would have much more work to use try/catch
in all functions that he used to not break the code.
So if not even PHP uses exceptions
often, when then should the developer use them? Is it preferable to use the same method used by the language, that is, to return false in the functions and treat them when necessary? Or always shoot a exception
when the input is not as expected?
Have you read this? http://answall.com/questions/21767/por-que-devemos-evitar-retornar-c%C3%b3digos-de-error
– Maniero
I believe that we should always seek to use it as a way to prevent and identify errors. Personally I use whenever I use O.O. in my code, as well as in database queries.
– Gustavo Emmel