Home > Article > Backend Development > The difference and application of error and exception in php, errorexception_PHP tutorial
The difference between error and exception. Most of the information online is explained by Java. It seems that the exception handling process of PHP is similar to that of Java
The Object inheritance structure in java is as follows:
Object---->Throwable--------> Exception ----> RuntimeException | Error
Errors are all unchecked types. Exceptions are divided into checked and unchecked types
And treat exceptions and errors as symptoms of abnormal program operation
If you distinguish between exceptions and errors:
Exception: Non-fatal. try{}catche(Exception e){} The try module being executed is a test run. If an error (non-fatal error) occurs during the running of the code, execute catch
Exceptions work like the following code:
if(mysql_connect('127.0.0.1','root','321321')) { echo '连接数据库成功'; // other code... } else { echo '连接数据库错误'; return false; }
Exception handling can be used to easily handle exceptions. For example, the following code can handle many exceptions at once
try { mysql_connect('127.0.0.1','root','321321'); // other code you want to execute }catche(Exception $e){ print_r($e); }
Error: fatal. Usually it is a program syntax error or a user-level prompt error
Errors and exceptions are divided into checked and unchecked
checked can be processed by the user, unchecked cannot be processed
Exception in php, user-level errors can be handled by the user (client code) other errors cannot be handled by the user
In addition, there is a RuntimeException in Java that cannot be handled by the user. This is a run-level exception
error indicates a serious problem in a situation where recovery is not impossible but difficult. For example, memory overflow. It is impossible to expect a program to handle such a situation.
exception represents a design or implementation problem. That is, it represents a situation that would never occur if the program were running normally.
This is a professional answer to the interview question.
error indicates a serious problem in a situation where recovery is not impossible but difficult. For example, memory overflow. It is impossible to expect a program to handle such a situation. exception represents a design or implementation problem. That is, it represents a situation that would never occur if the program were running normally.