The difference between PHP exceptions and errors: 1. PHP errors are problems that belong to the PHP program itself, usually caused by illegal syntax and environmental problems; 2. PHP exceptions are generally unexpected occurrences in business logic , a situation that is different from the normal process is not a grammatical error.
The operating environment of this tutorial: Windows 7 system, PHP version 5.6, DELL G3 computer.
The difference between PHP exceptions and errors:
PHP errors: is a problem that belongs to the PHP program itself, usually due to illegal syntax and environment The problem causes the compiler to fail the check or even fail to run. The warnings and notices you usually encounter are all errors, but they are at different levels.
PHP exception: Generally, it is an unexpected situation in the business logic that is different from the normal process. It is not a syntax error.
// 以除数为0为例,看看PHP是如何处理的 <?php $a = 0; try { echo 4/$a; } catch (Exception $e){ echo $e->getMessage(); } //报错,PHP是无法自动捕获异常的,必须手动抛出 $a = 0; try { if($a == 0){ throw new Exception('除数不能为0', 1); } echo 4/$a;//不会执行 } catch (Exception $e){ echo $e->getMessage(); }
The exception mechanism used in PHP is generally used for business logic judgment. When the business logic is abnormal, special processing is performed.
Let’s take a look at the PHP error handling mechanism. PHP provides the set_error_handler() function for error handling. When an error occurs, set_error_handler will handle the error uniformly.
Related video recommendations: PHP programming from entry to proficiency
The above is the detailed content of What is the difference between php exception and error. For more information, please follow other related articles on the PHP Chinese website!