Home  >  Article  >  Backend Development  >  PHP7 错误处理

PHP7 错误处理

WBOY
WBOYOriginal
2016-06-06 20:22:521024browse

当前PHP已经发布到7版本了
前两天也研究了一下
现在对新的错误处理和异常处理方式不是很明白怎么用
希望大神能给个示例

<code>    try {
        not_exists_func();
    } catch (\EngineException $e) {
        echo $e->getMessage();
    }</code>

以上的代码并不能抛出异常, 是为什么呢?

回复内容:

当前PHP已经发布到7版本了
前两天也研究了一下
现在对新的错误处理和异常处理方式不是很明白怎么用
希望大神能给个示例

<code>    try {
        not_exists_func();
    } catch (\EngineException $e) {
        echo $e->getMessage();
    }</code>

以上的代码并不能抛出异常, 是为什么呢?

PHP7实现了一个全局的throwable接口,原来的Exception和部分Error都实现了这个接口(interface), 以接口的方式定义了异常的继承结构。于是,PHP7中更多的Error变为可捕获的Exception返回给开发者,如果不进行捕获则为Error,如果捕获就变为一个可在程序内处理的Exception。这些可被捕获的Error通常都是不会对程序造成致命伤害的Error,例如函数不存。

\这个是干嘛的

不是应该new么? 怎么成/这个了

尝试用Exception抓取下

<code><?php try {
    not_exists_func();
} catch (EngineException $e) {
    var_dump($e->getMessage());
}

output:
string(44) "Call to undefined function not_exists_func()"

</code>

打开错误报告?

<code>ini_set("display_errors", 1);
error_reporting(E_ALL^E_NOTICE);</code>
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