Error handling mechanism modification
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.
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. The construction method of internal objects will always throw an exception if it fails.
The construction method of internal objects will always throw 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.
##PHP 7 Error Handling
PHP 7 changes the way most errors are reported. Unlike PHP 5's traditional error reporting mechanism, most errors are now thrown as Error exceptions. This kind of Error exception can be caught by try / catch block like a normal exception. If there is no matching try / catch block, The exception handling function (registered by set_exception_handler()) is called for processing. If an exception handler has not been registered, it is handled in the traditional way: it is reported as a Fatal Error. The Error class is not extended from the Exception class, so code like catch (Exception $e) { ... } cannot be caught. to Error. You can use code like catch (Error $e) { ... } or by registering an exception handling function ( set_exception_handler()) to catch Error.
Error Exception Hierarchy
1 .Error
- ArithmeticError
- AssertionError
- DivisionByZeroError
- ParseError
- TypeError
##Example
<?php class MathOperations { protected $n = 10; // 求余数运算,除数为 0,抛出异常 public function doOperation(): string { try { $value = $this->n % 0; return $value; } catch (DivisionByZeroError $e) { return $e->getMessage(); } } } $mathOperationsObj = new MathOperations(); print($mathOperationsObj->doOperation()); ?>The execution output of the above program is:
Modulo by zero
PHP 7 exception
PHP 7 exceptions are used for backward compatibility and enhancement of the old assert() function. It enables zero-cost assertions in production environments and provides the ability to throw custom exceptions and errors. The old version of the API will continue to be maintained for compatibility purposes. assert() is now a language construct that allows the first parameter to be an expression, not just a string to be evaluated or a The boolean to be tested. assert() configuration1 |
| |
0 |
|