This is more a question of best practice than anything. Ive a fairly good grasp on how to use and extend exceptions in php, my question is where do we use them?
Say I have a method add($name, $pass) in a class user(). This method checks to see if the givin username exists, and if not, adds a new user. Now obviously, there is a few reasons such a method might fail. Firstly, the db connection (setup in the class's __construct()) might fail. Secondly the username may already exist, and thirdly, well maybe the query just fails.
I think its appropriate to throw an exception upon failing to connect to the database, and also on the query failing, but Ive mixed feelings about using exceptions in the case of the username already existing.
I can see that by using exceptions here it would be easy to format a message to the user especially if I build my own exception class's for each possible error. It just seems to me that having a username already exist isn't really what I would consider an error as such.
I think I would like to use exceptions in these cases, Im just not sure if its the done thing. Is it right to use exceptions as such control structures? Is there a best practice for this sort of thing?
ps: I work with php5 exclusivly so thats not a concern. Also (obviously) I would be returning false if not an exception.
Thoughts?