PHP try-catch

PHP try and catch are the blocks with the feature of exception handling, which contain the code to handle exceptions. They play an important role in exception handling. There is one more important keyword used with the try-catch block is throw. The throw is a keyword that is used to throw an exception.

Each try block must have at least one catch block. On the other hand, a try block can also have multiple catch blocks to handle various classes of exception.

Syntax

The following syntax is used in exception handling to handle runtime errors -

Let's learn about try-throw-catch in more detail:

try

The try block contains the code that may contain an exception. An exception raised in try block during runtime is caught by the catch block. Therefore, each try block must have at least one catch block. It consists of the block of code in which an exception can occur.

Following points needs to be noted about the try:

  • The try block must be followed by a catch or finally block.
  • A try block must have at least one catch block.
  • There can be multiple catch blocks with one try block.

catch

The catch block catches the exception raised in the try block. It contains the code to catch the exception, which is thrown by throw keyword in the try block. The catch block executes when a specific exception is thrown.  PHP looks for the matching catch block and assigns the exception object to a variable.

Following points to be noted about the catch:

  • There can be more than one catch block with a try.
  • The thrown exception is caught and resolved by one or more catch.
  • The catch block is always used with a try block. It cannot be used alone.
  • It comes just after the try block.

throw

It is a keyword, which is used to throw an exception. Note that one throw at least has one "catch block" to catch the exception.

It lists the exceptions thrown by function, which cannot be handled by the function itself.

finally

It is a block that contains the essential code of the program to execute. The finally block is also used for clean-up activity in PHP. It is similar to the catch block, which is used to handle exception. The only difference is that it always executes whether an exception is handled or not.

The finally block can be specified after or in place of catch block. It always executes just after the try and catch block whether an exception has been thrown or not, and before the normal execution restarts. It is useful in the following scenarios - Closing of database connection, stream.

Example 1

Let's take an example to explain the common flow of throw and try-catch as well as finally block:

Output:

Exception Message: Value must be less than 1
It is finally block, which always executes.

Example 2

Output:

In the below output, you can see that when an odd number is passed, an exception message is displayed. On the other hand, when an even number is passed, another message is shown.

Output for ODD Number 
Exception Message: Passed number is an ODD Number
It is finally block, which always executes.

Output for EVEN Number
If you see this text, the passed value is an EVEN Number
It is finally block, which always executes.

Example 2: Custom Exception and Multiple catch statement

In the below example, we will create a custom exception class that extends the Exception class. Here, we will also use multiple catch blocks with a single try block.

Output:

Output for EVEN Number
If you see this text, the passed value is an EVEN Number 

Output for ODD Number 
Exception Message: ODD Number

Important Points:

  • Apart from PHP exception class and its subclasses, we can also create our own custom exception classes to handle uncatch exceptions.
  • With PHP 5.5 and above, finally block is used to handle exceptions. This block is always executed anyway, whether an exception is thrown or not.

Next TopicPHP Tutorial




Contact US

Email:[email protected]

PHP try-catch
10/30