Example analysis of modifying php7 error handling mechanism

Time:2020-10-3

This paper describes the modification of php7 error handling mechanism. For your reference, the details are as follows:

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 hinting may need to be adjusted.

2、 Some fatal and recoverable fatal errors were changed to throw error objects.

There are some fatal and recoverable fatal errors. The error object is now reported instead. Error objects are independent of exception and cannot be captured by normal try / catch. Editor’s note: to register error handling functions, please refer to the RFC below.

These recoverable fatal errors that have been turned into exceptions cannot be ignored by error handler. In particular, type hinting errors cannot be ignored.

3、 Syntax errors throw a parseError object

Syntax errors throw a parseError object that inherits from the error object. When dealing with eval(), for potentially error code, in addition to checking the return value or error_ get_ In addition to last(), the parseError object should also be captured.

4、 If the constructor of an internal object fails, it always throws an exception

If the constructor of an internal object fails, it will always report an exception. Previous constructors would return null or an unusable object.

5、 Some e_ The level of strict error has been adjusted.


PHP 7 error handling

PHP 7 changes the way most errors are reported. Unlike the traditional error reporting mechanism in PHP 5, most errors are now thrown as error exceptions.

This error exception can be caught by a try / catch block just like a normal exception. If there is no matching try / catch block, the exception handler is called (set by set_ exception_ Handler () register). If the exception handling function has not been registered, it is handled the traditional way: it is reported as a fatal error.

The error class is not extended from the exception class, so code such as catch (exception $e) {…} cannot catch the error. You can use code like catch (error $e) {…} or by registering an exception handler (set)_ exception_ Handler ()) to catch the error.

Error exception hierarchy

1.Error      

  • ArithmeticError      

  • AssertionError      

  • DivisionByZeroError      

  • ParseError      

  • TypeError     

2.Exception

1458887252-2773-exception-hiearchy.jpg

example

<?php c
lass MathOperations {  
 Protected $n = 10; // computes the remainder operation, divides by 0, and throws an exception  
public function doOperation(): string  {    
try {     
$value = $this->n % 0;     
return $value;    
} catch (DivisionByZeroError $e) {     
return $e->getMessage();    
}  
} } 
$mathOperationsObj = new MathOperations();
 print($mathOperationsObj->doOperation()); 
?>

The output of the above program is as follows:


Modulo by zero

PHP 7 exception

PHP 7 exceptions are used for downward compatibility and enhancement of the old assert() function. It can realize zero cost assertion in production environment and provide the ability to throw custom exceptions and errors.

Older versions of the API will continue to be maintained for compatibility purposes. Assert() is now a language structure that allows the first parameter to be an expression, not just a string to be evaluated or a Boolean to be tested.

Assert() configuration

Configuration item Default value Optional value
zend.assertions 1
  • 1 – generate and execute code (development mode)

  • 0 – generates code, but skips it at execution time

  • -1 – no code generation (production environment)

assert.exception 0
  • 1 – when the assertion fails, an exception object can be thrown. If no exception is provided, an assertion error object instance will be thrown.

  • 0 – use or generate throwable, just an object-based warning instead of throwing an object (compatible with PHP 5)

parameter

assertion

Assertions. In PHP 5, it is a string for execution or a Boolean value for testing. In PHP 7, it can be an expression that returns any value that will be used by the execution result 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 if the assertion fails and is enabled assert.exception The object will be thrown.

example

Will zend.assertions Set to 0:


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

The output of the above program is as follows:


Hi!

Will zend.assertions Set to 1, assert.exception Set to 1:

example


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

The output of the above program is as follows:


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

More about PHP related content interested readers can see the special topics of this site: summary of PHP error and exception handling methods, summary of PHP string usage, PHP array operation skills encyclopedia, PHP basic syntax introduction course, PHP object-oriented programming introduction tutorial, PHP + MySQL database operation introduction tutorial and PHP common database Summary of operation skills

I hope this article will help you with PHP programming.