There is no need to "do" or the "best" way to handle errors.
Generally speaking, there are two types of errors
- Those that are processed by other parts of the program. The user never sees or does not know about these errors, at least not in a direct form.
- Those that caused a sufficient failure that the user should be informed as such.
Note that none of them has anything to do with the specific PHP mechanisms that you will use to handle errors.
If you use exceptions ... Then I recommend using exceptions in all directions. Register an exception handler and let it do most of the work — including other PHP errors . Invalid login information?
class InvalidLoginException extends Exception { protected $message = 'Login information is incorrect. Please try again.'; }
Then you have a bunch of options to implement.
try { $user->login();
Or, if you decide so, let the exception handler do it. Perhaps in an even more flexible way
class ApplicationErrorException extends Exception{} class InvalidLoginException extends ApplicationErrorException { protected $message = 'Login information is incorrect. Please try again.'; }
Then in the exception handler
if ( $exception instanceof ApplicationErrorException ) {
But exceptions are not the only way, and some are not even considered a good way .
source share