Some built-in errors defined in PHP

不言
Release: 2023-04-03 08:46:02
Original
1277 people have browsed it

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 &#39;test&#39; . $arg;;
}
function testArithmeticError(){
	try {
        $value = 1 << -1;
    } catch (ArithmeticError $e) {
        echo &#39;show ArithmeticError:&#39;;
        echo $e->getMessage();
    }
}
function testAssertionError(){
    ini_set(&#39;zend.assertions&#39;, 1);
    ini_set(&#39;assert.exception&#39;, 1);
	try {
        assert(1>2);
    } catch (AssertionError $e) {
        echo &#39;show AssertionError:&#39;;
        echo $e->getMessage();
    }
}
function testParseError(){
    try {
        eval(&#39;asset(1>2)&#39;);
    } catch (ParseError $e) {
        echo &#39;show ParseError:&#39;;
        echo $e->getMessage();
    }
}
function testTypeError(){
    try {
        foo(123);
    } catch (TypeError $e) {
        echo &#39;show TypeError:&#39;;
        echo $e->getMessage();
    } 
}
function testpisionByZeroError(){
    try{
        1%0;
    }catch(pisionByZeroError $e){
        echo &#39;show pisionByZeroError:&#39;;
        echo $e->getMessage();
    }
}
function testArgumentCountError(){
    try{
        foo();
    }catch(ArgumentCountError $e){
        echo &#39;show ArgumentCountError:&#39;;
        echo $e->getMessage();
    }
}

//foo("ArithmeticError")();
//foo("AssertionError")();
//foo("ParseError")();
//foo("TypeError")();
//foo("pisionByZeroError")();
//foo("ArgumentCountError")();
?>
Copy after login

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!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template