Jump to content

simple error messages or exceptions


jimfog
 Share

Recommended Posts

I have a form where if an input does not satisfy validation checks an error message is displayed-here is an example for the password:

  $checkpass = check_passwd($_POST['password']);			    if ($checkpass== false) {				    $errors.= 'The password must be at least 6 characters long';				    $errorclass['passwsmall'] = true;			    }

The question is if for cases like the above is better using the exception class or just use the above. In another function that insert the user's credentials into the db(after the checks are done, as part of the registration)-I check if the e-mail in the db is the same with one the new user has supplied, and if this is the case then throw an exception:

$result = $conn->query("select * from credentials where email='" . $email . "'");		 if (!$result) {			 throw new Exception('problrm.....');		    return false;		 }	   if($result->num_rows>0) {			 throw new Exception('There is another user with same e-mail.');			 return false;

SO I have to make a decision for the error mechanism. What do you propose?The reason that I use an exception in the registration is because the above code is not mine-it is copied from a book. The convenience with the first method(in my case) is that error variable is echo straight to the form indicating which input requires attention.

Link to comment
Share on other sites

Exception wont stack up error messages if Errors are in same level. Exception is good for error handling as you can catch anywhere in call stack to handle error. But if you want to show stacked error Exception would not be good. like if you have registration page and you want to show user same time about invalid nick and invalid password same time using exception for both case it would not show two errors if you throw two of those errors from same call stack(method call,function call). as soon it encounters exception it will stop executing and keep on going upward of call stack until it finds a suitable exception handling catch block. The other exception is not even thrown, rest of the code will not be executed. So in those case you could collect the errors and make up a combined error to get thrown by an exception. 'Exception' is generic you would want to extend it to any specific exception to identify them, eg, 'UserGeneratedException'. Exception is best suitable for handling unexpected error. You would usually use exception to handle the unexpected occurance rather than just for error displaying directly.

Link to comment
Share on other sites

So you are suggesting(in other words) that I use the first case of error display-not use exception.

Link to comment
Share on other sites

can you give me an example where exception could be used-errors occurring unexpected.

Link to comment
Share on other sites

connecting to a database or 3rd party API, or uploading/moving files on the server.

Edited by thescientist
Link to comment
Share on other sites

I'd use exceptions if I'm programming a library that somebody else is going to use. When the programmer catches the exceptions they can choose how to respond to them.

Link to comment
Share on other sites

I'd use exceptions if I'm programming a library that somebody else is going to use. When the programmer catches the exceptions they can choose how to respond to them.
So...in this case-as far as I understand from your answer-where a check is made if there is a similar username in the db with the one that the user submitted in the form, I SHOULD not use exception, but just print an error message. Am i correct?
Link to comment
Share on other sites

The issue though....is that this same check requires also a connection to the database-an occasion requiring exception. so I think that I have to use both, but maybe I should HIDE the exception warning from the end user.

Link to comment
Share on other sites

  • 6 months later...

connecting to a database....

Does that include also the execution of queries?

Meaning to throw an exception when a query is unsuccessful?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...