In this lesson, you will learn more about PHP’s way to handle errors and catch exceptions. Handling errors and exceptions in a progressive manner isn’t difficult and somewhat similar to how other C++ or Perl handle errors or catch exceptions.

Error handling lets you catch errors caused by your code and taking appropriate action according to the scenario.

Handling Errors in PHP

There could be a number of reasons your code may not work as it is intended to and may cause errors. For example:

  1. The user gives an invalid input and your program doesn’t have any method or routine to process the invalid input.
  2. The web server failed to process your request.
  3. The file or database you are trying to access isn’t available or you don’t have the authorization to access these.
  4. You may be trying to access the unauthorized area

These types of errors usually occurred at run time. The other type of errors is syntax error and is supposed to be fixed long before the script is executed.

PHP Error Levels

In PHP, errors are defined at various error levels.

For every error, PHP generates an integer value and an associated constant number to identify the error.

Error level Value Description
E_ERROR 1 Fatal errors fall into this category. Execution of the program will be stopped if it encounters a fatal error.
E_WARNING 2 These are considered as warnings and not fatal errors. The execution of the script won’t be stopped due to this.
E_NOTICE 8 To show something has gone wrong, notices are generated by PHP.
E_USER_ERROR 256 This is like a fatal error, except that it is generated using the function trigger_error() rather than the PHP engine.
E_USER_WARNING 512 This is like a warning, except that is generated by function trigger_error()
E_STRICT 2048 When used error_reporting(E_ALL | E_STRICT), PHP will display all possible errors or warnings. Usually, this is used when you are in debugging mode.
E_ALL 8191 It will display all errors and warnings.

Difference between E_STRICT and E_ALL

E_ALL will display all types of errors. E_STRICT will display notice or suggestions of strict coding or best practice. Since PHP 5.4, E_STRICT has been included in E_ALL.

<?php

error_reporting(0); // Turn off all error reporting
error_reporting(-1); // This will turn on all errors

error_reporting(E_ERROR | E_WARNING | E_PARSE);
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
error_reporting(E_ALL & ~E_NOTICE); // This will report all errors except E_NOTICE
error_reporting(E_ALL); // This will display all errors

ini_set('error_reporting', E_ALL); //Will report all errors and warnings

?>

Termination of the code

Sometimes, when encountering an error, terminating the code execution is a better option.
Die() function is used to display a string message and terminate the script. The exit() function is an alias of PHP die() function.

Let’s say you have to establish a connection with a database.

$conn = mysqli_connect(“databaseName”, “root”, “”, “db_name”) or die(“Could not connect to the database”);
Die() function will terminate the code gracefully while displaying the message if the code fails to execute due to any reason.

Exception handling

Since PHP 5, a new model to catch errors and exceptions has been introduced. Handling errors through catch blocks is the same as other programming languages.

When a PHP exception is thrown, the catch statement handles the exception.

The syntax of exception handling in PHP

try{

// your code goes here
}catch(Exception $e){

// code to handle the exception

}finally{

//code that always runs. It is optional.

}

Try block contains the code that may throw an exception. Each line of the code will be executed until PHP encounters an exception. The throw keyword signals the exception. The PHP runtime will then find a catch statement that is supposed to handle the exception.

If an exception is thrown, catch block will be called by PHP. The code within the catch block handles the exception that was thrown. Since 5.5, the finally statement is also added to handle the exception. Code within the finally block will be executed after the try and catch block, no matter whether an exception has been thrown or not. You can have multiple catch blocks for try statement. This will allow you to customize code according to the exception being thrown.

The difference between error and exception

You must be wondering that what is the difference between these two and when we use error handling and when exception handling.

Exceptions are thrown and caught while errors are supposed to display a message or surpassed using @ symbol.

Exceptions are more OOP style so benefits are obvious.

PHP Date and Time Tutorial Home PHP and JSON

 

Last modified: April 28, 2019