Home > php教程 > PHP源码 > Detailed introduction to PHP7 error handling mechanism

Detailed introduction to PHP7 error handling mechanism

WBOY
Release: 2016-07-06 13:34:22
Original
1655 people have browsed it

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.


try {
Not_exists_func();
} catch (EngineException $e) {
var_dump($e->getMessage()); }


output:

string(44) "Call to undefined function not_exists_func()"

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.

These recoverable fatal errors that have turned into exceptions cannot be silently ignored through the error handler. In particular, type hint errors cannot be ignored.

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>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template