php - Factory Method - what exception to throw -
i have extremely simple factory method creates model instance based on values , conditions of config passed it.
what unsure if factory unable create model instance based on config receieves.
first question is, should return false or null? or should throw exception?
if should throw exception exception should be. im working in php here (list of php exceptions http://php.net/manual/en/spl.exceptions.php )
you person can answer questions. should happen isn't defined anywhere.
if ask me, prefer throw exception, call yourfactory_classnotfoundexception
if want. make calling code more straightforward won't need test return value.
afaik know there following common consensus in php core functions , classes:
false
should returned method encounters error , not throw exceptions. (and has no boolean return valueis_file()
). methods expected seterrno
,errmsg
give user chance grab error message.some of newer php classes throw exception in case. prefer exceptions library code.
null
returned if void method without return statement. can conditional