or die()
is an extremely primitive way to "handle" errors and only for examples or debugging at best. In practice, it depends on how you handle your errors. You may want to return false
from a function call or you may want to throw your own exception instead; e.g.:
if (!$con = mysql_connect(..)) {
throw new DatabaseConnectionError(mysql_error());
}
try..catch
will do exactly nothing with mysql
, since mysql
never throws any exceptions. It only ever returns false
on failure.
You will have to have your own error handling strategy. You'll probably want to log errors and display a user friendly error page instead of cryptic error messages. mysql
is not concerned with that part. It only gives you a way to check whether an operation was successful or not (check if it returns false
); what you do with this information is up to you. die
kills the entire application and at least doesn't allow the problem to propagate further; but it certainly does not display any user friendly error pages.
Having said all this, mysql
is old and deprecated. If you'd use something newer like PDO instead, it can properly throw exceptions itself.