Home > Article > Backend Development > Some built-in errors defined in PHP
Starting from PHP7, Error was introduced and some built-in errors were defined. Here we summarize some of the defined built-in errors, which can be regarded as a record.
ArithmeticError: Error subclass, thrown when an error occurs during mathematical operations. These errors in PHP7 include performing negative displacement operations and calling intp() resulting in values outside the integer range.
AssertionError: Error subclass, throws an exception when the assertion issued through assert fails. Only when ini sets zend.assertions and assert.exception to 1 and assertions are enabled, an AssertionError exception will be thrown when assert() is executed and is false.
ParseError: Error subclass, which will throw an exception when an error occurs in parsing PHP code.
TypeError: Error subclass when the parameter type passed to a function does not match its corresponding declared parameter type; the value returned from the function does not match the declared function return type; and an invalid number of parameters is passed in strict mode An exception will be thrown when giving the built-in PHP function.
pisionByZeroError: ArithmeticError subclass, throws an exception from intp() when the denominator is 0 or when 0 is used as the modulo operator (%). Using 0 in the division (/) operator will only issue a warning and the result will be NAN if the numerator is 0, and INF if the numerator is non-zero.
ArgumentCountError: Since PHP7.1, TypeError subclass throws an exception when the number of parameters passed to a user-defined function or method is less than the defined number of parameters.
<?php declare(strict_types=1); function foo(string $arg){ return 'test' . $arg;; } function testArithmeticError(){ try { $value = 1 << -1; } catch (ArithmeticError $e) { echo 'show ArithmeticError:'; echo $e->getMessage(); } } function testAssertionError(){ ini_set('zend.assertions', 1); ini_set('assert.exception', 1); try { assert(1>2); } catch (AssertionError $e) { echo 'show AssertionError:'; echo $e->getMessage(); } } function testParseError(){ try { eval('asset(1>2)'); } catch (ParseError $e) { echo 'show ParseError:'; echo $e->getMessage(); } } function testTypeError(){ try { foo(123); } catch (TypeError $e) { echo 'show TypeError:'; echo $e->getMessage(); } } function testpisionByZeroError(){ try{ 1%0; }catch(pisionByZeroError $e){ echo 'show pisionByZeroError:'; echo $e->getMessage(); } } function testArgumentCountError(){ try{ foo(); }catch(ArgumentCountError $e){ echo 'show ArgumentCountError:'; echo $e->getMessage(); } } //foo("ArithmeticError")(); //foo("AssertionError")(); //foo("ParseError")(); //foo("TypeError")(); //foo("pisionByZeroError")(); //foo("ArgumentCountError")(); ?>
Related recommendations:
Error level in php, php error level_PHP tutorial
The above is the detailed content of Some built-in errors defined in PHP. For more information, please follow other related articles on the PHP Chinese website!