Emanuil Rusev
Emanuil Rusev

Reputation: 35235

When to throw an exception and when to just return?

Sometimes functions fail to do what they say they do. When this happens, some functions throw an exception (e.g. PDO constructor) and others just return (e.g. mysql_connect would return false).

I have many times had to choose between one of these approaches over the other and I wonder if there is a design principle that deals with this.

Upvotes: 0

Views: 55

Answers (1)

Madara's Ghost
Madara's Ghost

Reputation: 174957

Returning false on error is considered to be an old practice, today, it's considered a bad one.

mysql_connect is a very old (2002) functions, before PHP implemented Exceptions or an OOP interface.

PDO is a newer, more recent (and more recommended) interface for accessing the database, and as such, it throws errors in the form of PDOExceptions.

Upvotes: 2

Related Questions