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


2.Exception

##...

1458887252-2773-exception-hiearchy.jpg

##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() configuration

Configuration itemsDefault valueOptional value zend.assertions1assert.exception0

Parameters

assertion

Assertion. In PHP 5, a string for execution or a boolean for testing. In PHP 7, this can be an expression that returns any value, and the result will be used to indicate whether the assertion was successful.

description

If the assertion fails, the option description will be included in the failure message.

exception

In PHP 7, the second parameter can be a Throwable object instead of a string, which will be thrown if the assertion fails and assert.exception is enabled.

Example

Set zend.assertions to 0:

<?php
ini_set('zend.assertions', 0);
assert(true == false);
echo 'Hi!';
?>

The above program execution output result is:

Hi!

Set zend.assertions to 1 and assert.exception to 1:

Example

<?php
ini_set('zend.assertions', 1);
ini_set('assert.exception', 1);
assert(true == false);
echo 'Hi!';
?>

The above program execution output results For:

Fatal error: Uncaught AssertionError: assert(true == false) in -:2Stack trace:#0 -(2): assert(false, 'assert(true == ...')#1 {main}
  thrown in - on line 2




  • 1 - Generate and execute code ( Development mode)

  • 0 - Generate code, but skip it during execution

  • -1 - Do not generate code (Production environment)


  • 1 - Thrown when an assertion fails, an exception object can be thrown, or an AssertionError object instance is thrown if no exception is provided.

  • 0 - Use or generate Throwable, just generate warnings based on the object instead of throwing the object (compatible with PHP 5)