It all depends on what you do. Personally, I use them all the time, so I don’t need to check the return value (a silly but illustrative example):
function ArrayToObject(array $array) { $obj = new StdClass(); foreach ($array as $key => $value) { if (!is_string($key)) { throw new Exception('Expects only string keys in the array'); } $obj->$key = $value; } return $obj; }
This way I can do:
$array = array('foo' => 'bar'); try { echo ArrayToObject($array)->foo;
This allows you not to worry about the error checking your results. You can handle errors directly in the catch block.
So no, don’t change what you are going to return based on exceptions ... Let exceptions handle errors and a modified workflow for you ...
A more realistic example (in pseudo-code):
try { open database connection; send query to database; operate on results; } catch (DatabaseConnectionException $e) { handle failed connection here; } catch (DatabaseQueryException $e) { handle failed query here; } catch (Exception $e) { handle any other errors here; }
Obviously, provided that your database functions / methods throw these exceptions ...
source share