Web Programming TutorialsLearn How to do Error Handling in PHP 7

Learn How to do Error Handling in PHP 7

In the last chapter, we discussed about ‘PHP 7 Exceptions with Expectations’ along with examples. In this chapter, we are going to discuss about new PHP 7 Error handling methodology that is added to PHP 7.

Error Handling in PHP 7
In PHP 7, error handing methodology has changed, now most of the errors are handled by throwing Error exceptions instead of reporting errors through the PHP 5.x traditional error reporting methodology as hierarchy. In PHP 7, similar to the exceptions these Error exceptions when obtained are directed to the matching catch block. In case no matching catch block is found, it will call a default exception handler that is installed with ‘set_exception_handler ()’. Again, if no default exception handler is found then the exception will be converted to the fatal error and in such a situation it will be handled like a traditional PHP 5.x error hierarchy.

In PHP 5.x, as the Error hierarchy is not extended from Exception, the catch block ‘catch (Exception $e) {…}’ that catches the exception cannot handle the errors. Therefore, in order to handle errors, either a catch block ‘catch (Error $e) {…}’ or a set_exception_handler () handler is required to handle such fatal errors. However, if we are using the catch block as ‘catch (Throwable $e) {…}’, then it has the capability of handling both Errors and Exceptions as Throwable, which is at the top of the hierarchy as shown below. As compared to PHP 5.x version of exception handling, the PHP 7 version has a more robust phenomenon of handling errors in the step wise approach as explained before.

Hierarchy of Throwable in PHP 5.x
Comparison between Errors and Exceptions

S.No. Errors Exceptions
1. All exceptions are errors. Reverse is not true.
2. All errors cannot be handled. All exceptions can be handled.
3. All errors cannot be handled at runtime. Exceptions can be handled at runtime.
4. Errors in the program are usually tend to signal the end of the program execution, they typically cannot be recovered from their error state and should cause to exit from the current program. Exceptions in program do not signal the end of program execution and could be handled or recovered from the exception state instead of exiting the current program.
5. Developers usually cannot guess errors. Exceptions can be guessed easily by developers and can be caught by using try catch block.

Exception and Error handling in PHP 7

Like other programming languages, PHP 7 has a similar exception or error handling model. There are three important keywords in this exception model as described below.

  • Try – The block of code or expressions which are anticipated to throw an error or exception should be enclosed inside the “try” block. This block will execute as a normal code block if no exception is thrown. If an exception is triggered inside the “try” block then it will throw an exception and pass the control to the exception or error matching catch block.
  • Throw – It triggers an exception. Each “throw” must have at least one “catch”.
  • Catch – The catch block has an exception or error class that matches with the type of exception that is thrown in the try block and invokes an object that contains the required exception information.

When a try block encounters an exception then the statements in the code that follows the exception statement will not be executed and the control will automatically be passed to the matching exception or error catch block. Once catch block is reached an exception object will be created and the statements which are present inside the catch block will be executed. If no matching catch block is found for the type of exception or error thrown by the statement in the code, then a PHP Fatal Error will be issued with an “Uncaught Exception”.

Following things to be noted for try and catch block in PHP 7

  • A block of code that throws exception should be surrounded by a try block
  • Each try block must have the following, at least one catch block which has the exception of error class defined in it. We may use multiple catch blocks to catch different classes of exceptions which are followed after the catch blocks.
  • We can re-throw the Exceptions within a catch block.


PHP 7 Example to throw and Exception or Error (here DivisionByZeroError)
Explanation of Code

  • Here, first of all we are defining an ‘AithmeticDivisonOperation’ class which has the data member as ‘$number’ that has assigned a value as 120.
  • Next, we have declared a function ‘doDivision’ that returns the result of division as a string. Since we know that there is a possibility of dividing a number with zero that is an undefined number. Therefore, we must catch this ‘DivisionByZeroError’ to prevent the program from going into fatal error in which it will call a default exception handler that is installed with ‘set_exception_handler ()’. Hence we are checking the division expression by zero inside the try block and catching the ‘DivisionByZeroError’ error as shown above. While catching such an error, we will display the error information returned by the ‘getMessage ()’ method of the ‘DivisionByZeroError’ class object which will be invoked automatically if ‘DivisionByZeroError’ exception will occur while executing the program.
  • Lastly, we are creating an object of ‘AithmeticDivisonOperation’ class and calling the function ‘doDivision’ using the class object.

When we execute the above PHP 7 program, we will observe the following output. In the output, we can observe a warning message of “Division by zero”. This explains ‘DivisionByZeroError’ error was thrown in the code that invoke a ‘DivisionByZeroError’ object. This object has returned the error information as “Division by zero” by calling ‘getMessage ()’ method of this class.
Source Code for Error Handling in PHP 7
In this chapter, we have discussed about the new PHP 7 Error handling methodology that is added to PHP 7 as compared to the ones in PHP 5.x. In the coming chapter of this tutorial, we are going to discuss the ‘use’ statement and Integer division which are added to PHP7.


  1. Dear eduonix.com I shared your article on my linkedin, would you be so kind to lend me your exceptions image and the table below for my blog (the post is not published yet)? I will add the message: Courtesy of https://www.eduonix.com

    Thank you very much


Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article


More article

- Advertisement -