从php7开始,便引入了error并定义了一些内置错误。在这里总结一些所定义的内置错误,也算是有个记录。
arithmeticerror:error子类,在执行数学运算发生错误时抛出。php7中这些错误包括执行负数位移操作、以及调用intp()导致的超出整数范围之外的值。
assertionerror:error子类,通过assert发出的断言失败时抛出异常。只有ini设置zend.assertions、assert.exception为1并且启用断言时,执行assert()时才会在为false时抛出assertionerror异常。
parseerror:error子类,当解析php代码发生错误时会抛出异常。
typeerror:error子类,当传递给函数的参数类型与它对应的声明参数类型不匹配;从函数返回的值与声明的函数返回类型不匹配以及在严格模式下将无效数量的参数传递给内置的php函数时会抛出异常。
pisionbyzeroerror:arithmeticerror子类,当分母为0或者当0被用作模运算符(%)时,从intp()中抛出异常。在除法(/)操作符中使用0只会发出警告,如果分子为0结果为nan,如果分子非0则结果为inf。
argumentcounterror:php7.1起,typeerror子类,当传递给用户定义的函数或方法少于定义的参数数量时抛出异常。
<?phpdeclare(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")();?>
相关推荐:
php中的错误级别,php错误级别_php教程
以上就是php中定义的一些内置错误的详细内容。