I have some classes in my code. From these classes I initiate objects, say
$c = new category ($myGoodObject->categoryId);
This should give the category object of myGoodObject. However, for example if a categoryId of say 1000 is not defined in database, the class throws an exception.
How to I catch this? And what is the best practice for initiating/loading objects from database that sometimes may not exist? How to avoid failure?
Thanks!
EDIT:
I tried this:
try {
$c = new category (1000);
} catch (Exception $e) {return false;}
but it didn’t work.
As for best practice, you should first attempt to read the database to see if the value exists, if not, you don’t attempt to create the object, therefore avoiding the need for exceptions.