Home  >  Article  >  Backend Development  >  Errors and Exceptions in PHP

Errors and Exceptions in PHP

WBOY
WBOYOriginal
2016-07-29 09:16:171489browse

Errors and exceptions are two completely different concepts

Errors

Error types

A total of 16

Values Constant Description Remarks
1 E_ERROR (integer) Fatal runtime error. This type of error is generally an unrecoverable situation, such as a problem caused by memory allocation. The consequence is that the script terminates and does not continue to run.
2 E_WARNING (integer) Runtime warning (non-fatal error). Only a prompt message is given, but the script does not terminate.
4 E_PARSE (integer) Compile time syntax parsing error. Parsing errors are generated only by the parser.
8 E_NOTICE (integer) Runtime notification. Indicates that the script encounters a situation that may appear as an error, but there may also be similar notifications in scripts that can run normally.
16 E_CORE_ERROR (integer) A fatal error that occurred during PHP initialization startup. This error is similar to E_ERROR, but is generated by the PHP engine core. since PHP 4
32 E_CORE_WARNING (integer) Warning (non-fatal error) occurred during PHP initialization startup. Similar to E_WARNING, but generated by the PHP engine core. since PHP 4
64 E_COMPILE_ERROR (integer) Fatal compile time error. Similar to E_ERROR, but generated by the Zend script engine. since PHP 4
128 E_COMPILE_WARNING (integer) Compile time warning (non-fatal error). Similar to E_WARNING, but generated by the Zend scripting engine. since PHP 4
256 E_USER_ERROR (integer) User-generated error message. Similar to E_ERROR, but is generated by the user using the PHP function trigger_error() in the code. since PHP 4
512 E_USER_WARNING (integer) User-generated warning message. Similar to E_WARNING, but is generated by the user using the PHP function trigger_error() in the code. since PHP 4
1024 E_USER_NOTICE (integer) Notification information generated by the user. Similar to E_NOTICE, but is generated by the user using the PHP function trigger_error() in the code. since PHP 4
2048 E_STRICT (integer) Enable PHP's suggestions for code modifications to ensure the best interoperability and forward compatibility of the code. since PHP 5
4096 E_RECOVERABLE_ERROR (integer) A fatal error that can be caught. It indicates that a potentially dangerous error has occurred, but has not caused the PHP engine to become unstable. If the error is not caught by a user-defined handler (see set_error_handler()), it will become an E_ERROR and the script will terminate. since PHP 5.2.0
8192 E_DEPRECATED (integer) Runtime notification. When enabled, a warning will be given about code that may not work properly in future versions. since PHP 5.3.0
16384 E_USER_DEPRECATED (integer) User generated warning message. Similar to E_DEPRECATED, but is generated by the user using the PHP function trigger_error() in the code. since PHP 5.3.0
30719 E_ALL (integer) E_STRICT all error and warning messages. 30719 in PHP 5.3.x, 6143 in PHP 5.2.x, 2047 previously

Error level

(1) Deprecated

Output

<code>if (ereg('/llo/', 'hello world')) {
    echo 'yes2';
} else {
    echo 'no2';
}
</code>
(3) Warning warning level

<code>Deprecated: Function ereg() is deprecated in /Users/weiheli/www/php/003.php on line 3
no
</code>
output

<code>$arr = ['a'=>'aaa', 'b'=>'bbb'];
echo $arr[a];
</code>
(4) Fatal error fatal level

<code>Notice: Use of undefined constant a - assumed 'a' in /Users/weiheli/www/php/003.php on line 4
aaa
</code>
output

<code>settype($var, 'abc');
echo $var;
</code>
(5) Parse error parsing error

<code>Warning: settype(): Invalid type in /Users/weiheli/www/php/003.php on line 3
</code>
output

<code>// 调用未定义的函数
fn(12);
</code>
(6) E_USER_ user Level errors

configuration errors

<code>Fatal error: Call to undefined function fn() in /Users/weiheli/www/php/003.php on line 3
</code>
can be set at runtime with the error_reporting directive

<code>echo 'hello world'
</code>
or can be set using the

ini_set

function

user-thrown errors

<code>Parse error: parse error, expecting `','' or `';'' in /Users/weiheli/www/php/003.php on line 3
</code>
loggingconfiguration in php.ini

<code>; 是否显示错误。解析错误始终都会显示
display_errors = On

; 显示哪些错误
;error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
error_reporting = E_ALL
</code>
Logging function

<code>error_reporting()
</code>
Customized error handling

<code>trigger_error(string $error_msg [, int $error_type = E_USER_NOTICE ])
</code>

Exception

<code>是否记录错误日志
log_errors = On

错误日志最大字节数
log_errors_max_len = 1024

是否忽略重复错误
ignore_repeated_errors

是否忽略重复信息来源
ignore_repeated_source

保存到系统日志中
error_log = syslog
</code>

As you can see from the above code, exceptions will not be automatically thrown in PHP. You must use

throw
, which is different from Java

<code>error_log()

openlog() 
syslog()
closelog() 
</code>
Built-in exception classes such as PDO are not used

throw

throwThe statements after will not be executed

try cannot be used independently

<code>set_error_handler — 设置一个用户定义的错误处理函数
</code>
Customized exception handling
<code>try {
    $num = 1 / 0;
} catch (Exception $e) {
    echo $e->getMessage();
}

Warning: Division by zero in /Users/weiheli/www/php/003.php on line 4
</code>

Copyright statement: This article is an original article by the blogger, No reproduction is allowed without the permission of the blogger.


The above has introduced the errors and exceptions in PHP, including related content. I hope it will be helpful to friends who are interested in PHP tutorials.

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