Difference and Application of error and exception in php

  • 2021-07-10 19:02:40
  • OfStack

Differences between error and exception Most of the online data are explained by java, and 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
Error is unchecked type Exception is divided into checked and unchecked type
And treat exceptions and errors as abnormal phenomena of program operation

If you distinguish between exceptions and errors:
Exception: Non-fatal. try {} catche (Exception e) {} The try module in execution is a test run, and the code runs with errors (non-fatal errors) to execute catche
Exceptions act like the following code:


if(mysql_connect('127.0.0.1','root','321321'))
{
   echo ' Connect to database successfully ';
   // other code...
}
else
{
   echo ' Error connecting to database ';
   return false;
}

Exceptions can be handled easily with exception handling. 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. 1 is usually a program syntax error or a user-level prompt error

Errors and exceptions are divided into checked and unchecked
checked can be handled by users, while unchecked cannot be handled
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 an RuntimeException in java that cannot be processed by users. This is a run-level exception


Related articles: