Handling PHP Errors and Exceptions

(Last Updated On: January 6, 2019)

1. Introduction

In this article in this series will learn how we can handle errors and exceptions that occur when our PHP code is executed.
We will learn about different types of errors and exceptions in PHP, how we can use built-in functions related to exception handling and how we can write custom error handler functions.

2. Types of Errors in PHP

Fundamentally, there are two types of errors in any software system:
Internal or Logical Errors: These types of errors are the errors which are in our programs. These are the logical errors in our code and can be avoided by careful programming.
External Errors or Runtime Exceptions: These type of errors occur at run-time. Some examples are the exception in connecting to a database or error in reading a file from the filesystem. As these are runtime errors and are mostly not known at the time of writing programs but we can always catch these exceptions using try-catch programming paradigm.

3. Error Reporting Settings in php.ini

Generally, in php.ini we set the default configuration of Error Reporting.
It can be done like:
We can also override this default value set in php.ini in our code using error_reporting()   function. Like:
// Turn off all error reporting

// Report simple running errors
error_reporting(E_ERROR | E_WARNING | E_PARSE);

// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

// Report all PHP errors (see changelog)

// Report all PHP errors

// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

4. Creating Global Custom Error Handlers

We can configure and override the types of errors that we want with PHP. However, we can also set the global error handler. The global error handler is a function which catches all the errors occurred during the execution of PHP program.
We can set any function to be global custom error handler using the function  set_error_handler(). Here is an example:
// error handler function
function errorHandler($errno, $errstr) {
  echo "Error: [$errno] $errstr";

// set error handler

// trigger an error
echo($foo); // undefined variable!

5. Exceptions Handling

In exception handling, there are specific keywords we need to know :
try: The try block has the code that may or may not throw an exception during execution. The code inside try is executed line by line until an exception is thrown.
throw: The throw keyword is used to create a new exception.  After an exception is thrown, the runtime try to catch the exception in any of catch statement.
catch: The catch block is executed only when an exception is, and the catch block is trying to catch the Exception or more specific exception.
finally: The finally block contains the code that needs to be always executed irrespective of any exception was encountered on not. There are specific tasks or code that should always be executed like the closing of DB connection, such tasks or code should go inside finally block. Example:
try {
    print "this is our try block n";
    throw new Exception();
} catch (Exception $e) {
    print "something went wrong, caught yah! n";
} finally {
    print "this part is always executed n";

6.Pre-defined classes for Exception Handling

PHP also has some predefined classes and interfaces that we can use and extend. The necessary hierarchy of pre-defined exception classes looks like :
 — Error
   — Arithmetic Error
   — Parse Error
 — Exception
   — Logic Exception
   — Runtime Exception

7. Conclusion

In this article, we had a quick look at different types of errors and how we can handle them in PHP. We also had a look at try-catch-finally constructs, and we can use it to catch exceptions. In our application, we can also define a global custom error handler, and we also learned about few predefined classed in PHP related to Exception hierarchy.


Please enter your comment!
Please enter your name here