Let's take a look at a detailed introduction to the error handling mechanism of PHP7. We have introduced quite a few tutorials on the new features of PHP7. We hope that the article can help all of you.
HP7 implements a global throwable interface. The original Exception and some Errors implement this interface (interface), and define the inheritance structure of exceptions in the form of interfaces. As a result, more Errors in PHP7 become catchable Exceptions and are returned to developers. If they are not caught, they are Errors. If they are caught, they become Exceptions that can be handled within the program. These catchable Errors are usually Errors that will not cause fatal damage to the program, such as a function that does not exist.
1. There are now two exception classes: Exception and Error.
PHP7 now has two exception classes, Exception and Error. Both classes implement a new interface: Throwable. In your exception handling code, type hints may need to be adjusted.
output:
2. Some fatal errors and recoverable fatal errors are changed to throw Error objects.
Some fatal errors and recoverable fatal errors now report Error objects instead. Error objects are independent from Exception, and they cannot be caught by regular try/catch. Editor's note: You need to register an error handling function, please refer to the RFC below.
3. Syntax errors will throw a ParseError object
Syntax errors will throw a ParseError object, which inherits from the Error object. When handling eval() before, in addition to checking the return value or error_get_last() for potentially error-prone code, you should also capture the ParseError object.
4. If the construction method of the internal object fails, it will always throw an exception
The construction method of internal objects will always report an exception if it fails. Some previous constructors would return NULL or an unusable object.
5. The levels of some E_STRICT errors have been adjusted. <script>ec(2);</script>