Error handling and Exception in PHP

Tram Ho

Hello everyone, this article I present about error handling and Exception in PHP, I am looking forward to your follow up.

1) Error handling

Error handling is the process of detecting errors generated when running your code and then performing the appropriate actions. If you handle the error incorrectly, it can lead to many unexpected results.

PHP provides mechanisms to help catch errors, monitor errors and handle errors that may occur when programming.

1.1) The die () and exit () functions

  • To catch errors in PHP we can use the die () or exit () functions.
  • When using die () or exit () functions, a message will be fired and the program will be stopped.

1.2) Self-defining functions to handle errors in PHP

  • To define the function itself to handle errors, you can use the syntax:

    error_function(errno, errstr, errfile, errline, errcontext);

  • The custom error handling function must have at least two required parameters (error_level and errstr), plus it has three additional parameters (errfile, errline and errcontext) that are not required, as described below.
    • errno: Determines the degree of error, as an integer. This corresponds to the appropriate error level constant (E_ERROR, E_WARNING, …)
    • errstr: Identify the error message as a string
    • errfile: Specifies the file name of the script in which an error occurred
    • errline: Determine the number of lines that an error occurs
    • errcontext: Defines an array containing all variables and their values ​​to exist at the time of the error. This parameter is very useful for debugging
  • Error levels in PHP
Error levelValueDescribe
E_ERRORfirstA serious Runtime error. The script execution is stopped immediately
E_WARNING2Non-fatal errors and script execution are not stopped
E_NOTICE8A runtime notification. Indicates that the script encountered an error that may occur, although the situation may also occur when running the script normally
E_USER_ERROR256An error message generated by the user. This is like an E_ERROR, except that it is created by a PHP script using the trigger_error () function rather than the PHP engine.
E_USER_WARNING512A non-fatal user generated warning message. This is like an E_WARNING, except that it is created by a PHP script using the trigger_error () function, not the PHP engine.
E_USER_NOTICE1024A user-generated notification notification This is like an E_NOTICE, except that it is created by a PHP script using the trigger_error () function rather than the PHP engine.
E_STRIC2048Not entirely an error, but triggered whenever PHP encounters code that can lead to incompatibility issues or transitions.
E_ALL8189All errors and warnings, except for E_STRICT before PHP 5.4.0.

For example

Thus, we customize the error message message to be more friendly and clear.

2) Exception

  • Exception is an object-oriented error management solution
  • It is a class built into the PHP language
  • An Exception object will contain information about where the error occurred (file name, line number)
  • Some keywords related to Exception
    • Try: the code with the exception of the try block is an exception. If no exceptions occur, the code will continue as usual. However, if an exception occurs, an exception will be thrown.
    • Throw: Exception is triggered. Each “throw” must include at least one “catch” block.
    • Catch: retrieve an exception and create an object containing exception information.

Syntax

For example :

  • The checkNum () function checks if a number is greater than 1. If an exception condition is thrown
  • The checkNum () function is called in a “try” block.
  • Exception in checkNum () function is thrown
  • The “catch” block retrieves the exception and creates an object ($ e) containing the exception information
  • The error message from the exception is repeated by calling $ e-> getMessage () from the exception object.

In the above example, the $ e-> getMessage function is used to get the error message. Here are some functions that can be used from Exception class in PHP.

  • getMessage (): Show an exception message
  • getCode (): Displays codes that are representative of exception types.
  • getFile (): Display the file name and path where the exception occurred.
  • getLine (): Displays the exception exception line.
  • getTrace (): Display returns error tracking information as an array of file names and line numbers
  • getPrevious (): Displays the exception before the current case occurs
  • getTraceAsString (): Displays a series of exceptions as a string instead of an array
  • __toString() : Display all exceptions as a string.

2.1 Create a custom exception class If the default Exception class is not enough for you, you can create your own exceptions by extending the Exception class.

  • Note that only the Exception’s __contruct() and __toString can override in the derived class, the rest cannot override because it is defined as final.

For example :

2. 2) Handling many exceptions

  • Multiple Exceptions use multiple try … catch blocks to try to catch thrown exceptions.

For example

2.3) Setting Global Exception Handling When an application generates an exception with a throw command, if the exception is not caught by the try … catch … block, the exception will pass to the foreign handler function PHP default rules. Now if you want to create this default function, do the following: Create your own exception handling function of the form my_exception_handler ($ exception), then use set_exception_handler (‘my_exception_handler’) to register with PHP

For example

End

So in this article I have presented about error handling and Exception in PHP thank you everyone for watching

Reference source

Share the news now

Source : Viblo