Home > Article > Backend Development > The difference and application of error and exception in php_PHP tutorial
This article mainly introduces the difference and application of error and exception in php. Friends in need can refer to it
The difference between error and exception. Most of the online information 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:
Anomaly: 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
The function of exception is similar to 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;
}
Using exception handling can 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
}catch(Exception $e){
print_r($e);
}