Back to Top

How to handle Exception in PHP5

Exception in PHP

Today I come with the new article which is about the OOPs concept in PHP. I am sure all developers have faced errors at the time of code execution(sometimes) because of malicious data and even perfectly good code may produce runtime errors so PHP has an option to deal with errors which are known as Exception.

Read about: Type Hinting in PHP5

Here is the topic about Exception in PHP which I am going to explain in this article:

1. What is Exception?
2. Main usage of Exception
3. Exception Handlers
4. Default Exception Handler function
5. Advantage and Disadvantage of the Exception
6. Extending the Exception Class
7. Example of the Extending Exception

What is Exception?

The exception is designed to handle situations when errors appear in the program. The exception is useful when runtime abnormal conditions arise in a program.Exceptions are introduced in PHP5.

Basic Syntax :

Here is a simple example that shows an Exception being thrown and caught:

Main Usage of Exception

Exceptions are an extremely powerful way to report run time error and robust error-handling method in PHP.You can use exception handling any kind of Possibilities and trigger an Exception

PHP have some exception handlers that display the exception. If you don’t use exception handler on your program and if any error occurs, it will stop your program execution but if you used an exception handler, program will continue to run with exception and it is known as exception handling.

Three Exception Handlers are used by exceptions::

Exception Handlers:

Predefined keywords for Exception in PHP are

1) Try-catch
3) Throw
4) Try-Finally

Let’s start with try-catch block for handling exception

1) Try-Catch:

Try is a block of code which caught an error and catch accept the exception thrown by try block and also handle the exception.


Here, If your code throws an error which is written in a Try block so execution of code is halted and it jump ahead to the catch block. Catch catches an Exception. The catch is normally used to perform the action that might be necessary from the error occurred.

Also Read: Static Keyword in PHP

NOTE: try statement without catch or finally is Invalid.


NOTE: You can’t use try on single statement. It must be surrounded by { } parenthesis.


Like above you can see, the exception will be a trigger for an invalid query syntax in the can enclose the entire code with a try/catch block and handle an error if one occurs.

You can use multiple catch blocks if you want to handle different errors differently, but you cannot use multiple try blocks, only one try block is allowed.

Here is the SYNTAX of multiple catch blocks: The syntax of try with multi-catch is as follows:

Here, When an exception is occurred the first catch() is called and if the result is true, the catch block is entered and the exception message display and If the result is false, the next catch statement is checked.

2) Throw

Throw is a new exception used to generate an exception manually in an own code.After detect an error using the throw statement, you can easily return to the exact point in the code which is responsible to handle execution of the program and continue to code execution because throw executes until it detects an appropriate try/catch block.

The catch block catches the exception and if any new exception occurs and catch block fails to handle that exception uncaught error occur. So using throw, You can do this by throwing an exception to catch block.

The throw statement


To throw an object which does not inherit from class Exception will result in a final runtime error.

NOTE : throw can only throw an object. You can’t throw strings or integers.

3) Try – Finally

Finally is also code block which will be executed if an exception occurs or not. This is a code block which gives assurance to be executed at least once when your program execute if the program has an any error or not.


All of the above methods are valid. You can use any of method. It’s just a matter of taste and style.

If the exception is not caught anywhere of any of above method PHP have another option to achieve exception is exception-handling function set_exception_handler():

Default Exception Handler function:

An interesting feature in PHP is that PHP has an ability to install a default exception handler that will be called if an exception reaches the top scope and still has not been caught. Default Exception Handler is totally different from a try-catch block. it is a single function that will handle any uncaught exception.

The default exception handler is very useful when you want to prevent a user from being returned an error of an uncaught exception.

You can define a function to set a default exception handler with a single parameter:

You can use above function into set_exception_handler:

set_exception_handler is replaced built-in behavior of exception when an uncaught exception occurs to the page.This handler has the ability to handle certain error conditions differently.

And to restore the previously defined exception handler following function is used:

Default Exception Handler gives the flexibility to handle an error instead of wrapping every statement into an individual try block.

Advantages and Disadvantages:


1. You don’t have to place if() statements in every place where an exception might occur.
2. Exceptions are greatly increased the reliability of your code if properly used.


1. PHP does not support the use of throws() methods like Java and other languages.
2. Unlike other languages, native PHP errors are displayed to the client-side are not mapped to exceptions yet

Extending the Exception Class

PHP has a built-in Exception class which is specifically designed for exceptions. In a simple manner, Exceptions are just objects that you can “throw” to PHP. PHP allows to define own classes that inherit from the inbuilt Exception class.

Example of Extending the Exception Class

The following code snippet shows how to extend the built-in exception class

The exception is a huge topic and here it just outlined so you can start using them immediately.I hope above explanation, You will understand the concept and put it to good use in your code. If you have any query, please share in the comment section. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Angular JS MVC Architecture with example

Posted on 3 years ago


GoTo statement in PHP

Posted on 6 years ago


An Introduction to Kubernetes

Posted on 2 years ago


To Swap Database Row in PHP

Posted on 6 years ago