» » PHP errors and exception handling

 

PHP errors and exception handling

Author: bamboo06 on 9-06-2020, 20:26, views: 2549

0
Unlike other programming languages ​​that throw exceptions when encountering errors, PHP also has an exception mechanism when handling objects, but PHP will perform as happily as possible and ignore what happened, unless it encounters an extremely serious error. There is an exception. This article outlines PHP-related error exception handling mechanisms.
PHP errors and exception handling


Error level
PHP has several error severity levels. The three most common types of information are errors, notices, and warnings. They have different severity: E_ERROR, E_NOTICE and E_WARNING. The error is a serious problem during operation, usually caused by code error, it must be corrected, otherwise PHP will stop execution. Notifications are information of a recommended nature, because the program code may cause problems during execution, but the program will not stop. The warning is a non-fatal error, and program execution will not be aborted.

Using PHP's built-in function error_reporting(), you can set the error level during program execution by passing in a predefined error level constant, which means that if you only want to see warnings and errors-not notifications-you can do this set up:
error_reporting(E_ERROR | E_WARNING);


You can let PHP use the error control operator @ to suppress specific errors, such as @fopen(). Place this operator before the expression, and any subsequent errors will not appear. But I don't recommend it.

error report
The error log is very helpful to find errors in the program, but sometimes it will also expose the structure of the application to the outside. In order to effectively protect your application from the problems caused by it.

In a development environment, I like to have PHP display and log all error messages, while in a production environment, I will let PHP log most error messages, but not display them. No matter how you do it, you must follow the following 4 rules:

Be sure to let PHP report an error.
An error is displayed in the development environment.
Errors cannot be displayed in the production environment.
Errors should be recorded in both the development environment and the production environment.
I set the error reporting method for the development environment in php.ini as follows:
;Display error
display_errors = On
display_startup_errors = On
;Report all errors
error_reporting = -1
; Record error
log_errors = On


I set up the error reporting method for the production environment in php.ini as follows:
; Do not show errors
display_errors = Off
display_startup_errors = Off
;Report all other errors except for precautions
error_reporting = E_ALL & ~E_NOTICE
; Record error
log_errors = On

Exception catch
Exceptions are standard in many popular programming languages, but they are often overlooked by PHP developers. For example, Ruby is a language that attaches great importance to exceptions. No matter what error occurs, such as HTTP request failure or database query problems, or even an image resource cannot be found, Ruby (or the gems used) will be thrown. Abnormal, you can immediately know what happened through the screen.

PHP is more casual in dealing with this problem, and calling the file_get_contents() function usually only gives FALSE values ​​and warnings. Many older PHP frameworks such as CodeIgniter simply return false, write the information to a proprietary log, or let you use a method like $this->upload->get_error() to view the cause of the error. The problem here is that you have to find out what the error is, and look through the documentation to see what kind of error method is used by this class, rather than explicitly exposing the error.

Another problem occurs when the class automatically throws an error to the screen and ends the program. Doing so will prevent other developers from dynamically handling errors. Exceptions should be thrown to make developers aware of the existence of errors, so that they can choose how to deal with them, for example:
<?php
$email = new Fuel\Email;
$email->subject('My Subject');
$email->body('How the heck are you?');
$email->to('[email protected]','Some Guy');

try
{
     $email->send();
}
catch(Fuel\Email\ValidationFailedException $e)
{
     // verification failed
}
catch(Fuel\Email\SendingFailedException $e)
{
     // This driver cannot send email
}
finally
{
     // No matter what kind of exception is thrown, it will be executed and executed before the normal program continues
}

Category: PHP Scripts

Dear visitor, you are browsing our website as Guest.
We strongly recommend you to register and login to view hidden contents.
Information
Comment on the news site is possible only within (days) days from the date of publication.