Home>Article>Backend Development> What errors does php exception handling catch?

What errors does php exception handling catch?

王林
王林 Original
2019-09-23 13:14:27 2645browse

What errors does php exception handling catch?

There are three types of error reports in PHP:

1. Error, syntax parsing error, fatal error

2. Warning

3. Pay attention to

Consequences:

Error-> Fatal error will terminate the execution of the downloaded program. If there is a syntax error, PHP will not work at all. It was not implemented.

Warning -> It will not terminate the run, but it will affect the results.

Note -> will not terminate execution and will not affect the results.

In order to give users a better experience, we mask all error output, which is output, not display. But in this case, the administrator will not be able to see the error. The error is not displayed on the page, but a log is generated for the administrator to view.

error_reporting(~E_ALL)All output is blocked. Naturally, the administrator cannot see it. I don't want to block all output, I just want to block all displayini_set('display_errors','off');means that all error displays on the page are blocked, but error output is not blocked.

ini_set('log_errors','on'); //开启日志写入功能 ini_set('error_log','myerror.log'); //日志的存放位置 ini_set('display_errors','off'); //屏蔽页面显示 error_reporting(E_ALL); //输出所有错误 echo 123; echo $str; //这个会出现一个注意,表示未声明变量 echo date(); //警告,没有传参 echo dae(); //致命错误,找不到这个函数 echo 123;

Exception:

In the PHP language, all exceptions must be thrown by themselves, unlike languages like JAVA that will automatically throw exceptions. This is also This is one of the reasons why exception handling statements are rarely seen in PHP source code.

Exceptions and errors:

Exceptions refer to conditions that do not meet expectations and are different from the normal process during program operation. The error is its own problem, which is caused by illegal syntax or environmental problems that prevent the compiler from passing the check settings and running.

Because PHP did not have exception handling at the beginning, it was later imitated in order to enter enterprise-level development. Once PHP encounters abnormal code, in most cases, it will directly throw an error instead of an exception.

php can only use try...catch to catch exceptions after you throw an exception (this is generally the case, and some exceptions can be automatically caught).

In PHP, exceptions are usually used in the following scenarios:

1. Pessimistic prediction of the program: If you think that your code cannot handle various possible problems one by one Foreseeable circumstances, unforeseeable circumstances.

2. Program needs and business concerns: If the consistency of data is very high, you can use try...catch to minimize the logical interruption damage caused by exceptions and remediate them

After processing, the integrity of the business logic will not be affected.

3. Robustness requirements at the language level: By accurately controlling the runtime process, when the program is interrupted, use try...catch predictably to narrow the scope of possible errors, catch exceptions in a timely manner, and

Provide corresponding remedies.

Errors in PHP:

Errors are situations that cause the script to run abnormally.

The main error levels in php are as follows:

deprecated: 最低级别的错误,表示"不推荐, 不建议"。例如在php 5中使用了ereg系列的正则函数就会出现。这类错误一般由于使用了不推荐的、过时的函数或语法造成。不影响程序正常运行,但建议修正。 notice: 一般指语法中存在不恰当的地方。如使用变量但是未定义就会报此错误。不影响程序正常流程。 warning: 较高级别的错误,在语法中出现很不恰当的情况才会出现此错误,比如函数参数不匹配。会导致得不到预期的结果,需要修改代码。 fetal error: 致命错误,直接导致程序终止运行。这类错误必须修改。 prase error: 语法解析错误,上面几种都属于运行时错误,此错误在运行前就会抛出。

Custom error handler:


You can use the set_error_handler() function to Managed error handler allows you to customize the error handling process.

If you want to cancel hosting, you can use restore_error_handler() in the same page to cancel hosting; if you want to throw an error yourself, you can use the trigger_error() function.

Recommended tutorial:

PHP video tutorial

The above is the detailed content of What errors does php exception handling catch?. For more information, please follow other related articles on the PHP Chinese website!

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
Previous article:What does php end with? Next article:What does php end with?