Home  >  Article  >  Backend Development  >  try-catch - PHP异常处理, 1除以0

try-catch - PHP异常处理, 1除以0

WBOY
WBOYOriginal
2016-06-06 20:09:391538browse

try{

<code>$a = 1/0;</code>

}catch(Exception $e){

<code>file_put_contents("filelog.log", $e.getMessage(), FILE_APPEND);</code>

}

执行后日志没内容,为什么?try catch能捕获什么类型的错误?

回复内容:

try{

<code>$a = 1/0;</code>

}catch(Exception $e){

<code>file_put_contents("filelog.log", $e.getMessage(), FILE_APPEND);</code>

}

执行后日志没内容,为什么?try catch能捕获什么类型的错误?

try {} catch {}PHP 5之后添加的机制,由于与原来的错误机制切换还需要过渡期,所以在PHP 5整个生命周期中,这两个错误机制是并存的,并且绝大部分核心错误仍然是Error形式报出的。不过在大部分扩展中的错误逐渐已经转变成了Throw抛出,并且PHP 7中也有很多核心错误以Throw抛出,不过在完全切换到Throw机制之前,最好做好两种机制并存的处理。

Division by zero 这是 warning 不是 Exception.

使用 set_error_handler 可以捕获

<code>set_error_handler(function($errno, $errstr) {
    var_dump($errstr);
});

$a = 1/0;</code>

这种异常不建议捕获,写程序的时候考虑仔细些就行了。

执行之前 判断除数 是否为空值0 做这种判断

mysql 也可以执行 1/0 ! 结果为null而已

Statement:
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