I'm working on an Application and a question occurred. I was thinking of letting PHP errors a side (they would be log in the database or in a file) and manage other errors (such as "Your username is not valid" or "Your typed the wrong password" or "The image could not be loaded") with the so famous try-catch method. Is it good to completely manage this kind of errors only with try-catch?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
I use a custom Log class I wrote, and set it up as the default error handler and exception handler, as well as providing "debug", "info", "warning", "success" and "deprecated" methods for manually logging (and optionally displaying) messages.
The bonus is, it shows a dump of the variables in the current scope when an actual error occurs, rather than just telling you where it occurred.
I think: It is not, because this are not errors, or exception, but simply invalid input. They are also quite common and in this context not an exception in the meaning of the word.
On the other hand, because every exception breaks the current execution, it may lead to unexpected behaviour, when some code is not executed, just because someone entered an unknown username or something. In case of errors, or exceptions, you usually want to stop the execution, because you assume that the execution is not (reasonable) possible (until you catch the exception and go on).
The main advantage of exceptions is it's behavior of "petite mort", a local
die()
insidetry{}
block, preventing further code from execution.While it's quite handy in handling application errors, it become not so good when dealing with validating user input. If a user made 3 mistakes filling a form, it would be merciful to show them all at once, not one after another.
You rather need POST/Redirect/GET pattern to handle user errors:
Errors and exceptions are a technical matter while validation it's business mater. IFs handle Business, TRY/CATCH handles technical stuff they do not mix and you should not mix them. There are times when you must use an if which throws an exception (PHP stuff) but that is the necessity at framework/architecture level for example
this way you can better control the errors. But the rule stays: Try/Catch/Throw = technical If/Else Switch = business
For enduser errors you should introduce a separate application level handling system.
Exceptions are useful for handling error conditions (errors that arise on production), while custom E_USER_ errors are the best fit for signaling data/debug/security issues (anything that should be handled at development stage).