Exception Handling in PHP
Exception handling is a powerful mechanism of PHP, which is used to handle runtime errors (runtime errors are called exceptions). So that the normal flow of the application can be maintained.
The main purpose of using exception handling is to maintain the normal execution of the application.
What is an Exception?
An exception is an unexcepted outcome of a program, which can be handled by the program itself. Basically, an exception disrupts the normal flow of the program. But it is different from an error because an exception can be handled, whereas an error cannot be handled by the program itself.
In other words, - "An unexpected result of a program is an exception, which can be handled by the program itself." Exceptions can be thrown and caught in PHP.
Why needs Exception Handling?
PHP provides a powerful mechanism, exception handling. It allows you to handle runtime errors such as IOException, SQLException, ClassNotFoundException, and more. A most popular example of exception handling is - divide by zero exception, which is an arithmetic exception.
Note: Exception handling is required when an exception interrupts the normal execution of the program or application.
Exception handling is almost similar in all programming languages. It changes the normal flow of the program when a specified error condition occurs, and this condition is known as exception. PHP offers the following keywords for this purpose:
The try block contains the code that may have an exception or where an exception can arise. When an exception occurs inside the try block during runtime of code, it is caught and resolved in catch block. The try block must be followed by catch or finally block. A try block can be followed by minimum one and maximum any number of catch blocks.
The catch block contains the code that executes when a specified exception is thrown. It is always used with a try block, not alone. When an exception occurs, PHP finds the matching catch block.
It is a keyword used to throw an exception. It also helps to list all the exceptions that a function throws but does not handle itself.
Remember that each throw must have at least one "catch".
The finally block contains a code, which is used for clean-up activity in PHP. Basically, it executes the essential code of the program.
What happens when an exception is triggered -
Advantage of Exception Handling over Error Handling
Exception handling is an important mechanism in PHP, which have the following advantages over error handling -
Grouping of error types -
In PHP, both basic and objects can be thrown as exception. It can create a hierarchy of exception objects and group exceptions in classes and also classify them according to their types.
Keep error handling and normal code separate -
In traditional error handling, if-else block is used to handle errors. It makes the code unreadable because the code for handling errors and conditions got mixed. Within the try-catch block, exception keeps separate from the code and code become readable.
Learn with the help of examples how exception handling works in PHP-
Let's take an example to explain the common flow of throw and try-catch block:
Exception Message: Value must be less than 1
Example 2: Creating Custom Exception
You can create user-defined exception by extending Exception class. Look at the code below to learn how create user-defined exception -
Result of division = 6 Divide by Zero Exception! Divide by Negative Number Exception!
To learn more about try-catch block in detail, click here.
Next TopicPHP try-catch