group-icon
PHP Programming
Group for all things PHP, Coding Help, Problems and Solutions
108 Members
Join this group to post and comment.
Shruthi Pillai
Shruthi Pillai
Information Technology
19 Jan 2019

Error handling in PHP

Error handling should be done properly with exceptions.

A lot of errors in our applications are exceptions. They are exceptional cases in our applications, which we should handle directly as soon as they occur.

PHP introduced Exception class in PHP 5.

An exception is "thrown" when an error occurs:

throw new Exception('' No more stock")

And an Exception class ought to be handled in a catch clause:

catch (Exception $e)

{

echo 'Çaught exception :'', $e->getMessage();

}

To make sure some code always runs such as closing a database connection, we use a finally clause:

finally

{ //close PDO connection}