What is try catch finally in php?

Summary: in this tutorial, you’ll learn how to use the PHP try...catch...finally statement to handle exceptions and clean up the resources.

Introduction to the PHP try…catch…finally statement

The try…catch statement allows you to handle exceptions. When an exception occurs in the try block, the execution jumps to the catch block. In the catch block, you can place the code that handles the exception.

The following example uses the try…catch block to read a CSV file into an array:

$data = []; try { $f = fopen('data.csv', 'r'); while ($row = $fgetcsv($f)) { $data[] = $row; } fclose($f); } catch (Exception $ex) { echo $ex->getMessage(); }

Code language: HTML, XML (xml)

In this example, if an error occurs while reading the file, the execution jumps to the catch block. Therefore, the following statement will never run:

fclose($f);

Code language: PHP (php)

When the file is not closed properply, it may be inaccessible later.

To ensure that the file will be closed properly regardless of whatever exception occurs or not, you can close the file in the finally block like this:

$data = []; try { $f = fopen('data.csv', 'r'); while ($row = $fgetcsv($f)) { $data[] = $row; } } catch (Exception $ex) { echo $ex->getMessage(); } finally { if ($f) { fclose($f); } }

Code language: HTML, XML (xml)

The finally is an optional block of the try…catch statement. The finally block always executes after the try or catch block.

In this case, if an exception occurs while reading the file, the execution jumps to the catch block to handle it and then the finally block executes to close the file.

The syntax of the try...catch...finally block is as follows:

try { // do something } catch (Exception $e) { // code to handle exception } finally { // code to clean up the resource }

Code language: HTML, XML (xml)

PHP finally & return

The following defines the divide() function that returns the division of two numbers. If an error occurs, it returns null.

function divide($x, $y) { try { $result = $x / $y; return $result; } catch (Exception $e) { return null; } finally { return null; } }

Code language: HTML, XML (xml)

However, the following displays NULL instead of 5:

// ... $result = divide(10, 2); var_dump($result); // NULL

Code language: HTML, XML (xml)

Typically, the return statement immediately stops the execution and returns a value. However, it doesn’t work that way when it is used with the try...catch...finally statement.

When you use the return statement with the try...catch...finally statement, the finally block still executes after the return statement.

The result will be returned after the finally block is executed. Also, if the finally block has a return statement, the value from the finally block will be returned.

In the try...catch..finally statement, either the catch or finally block is optional. Therefore, you can have the try...finally statement like this:

try { // do something } finally { // clean up }

Code language: HTML, XML (xml)

The try...finally statement allows you to throw an exception while cleaning up the resource appropriately.

Summary

  • Use the try...catch...finally statement to handle exceptions and clean up the resources.
  • The finally block always executes after the try or catch block.
  • If the try or catch has the return statement, the value will be returned only after the finally block executes.

Did you find this tutorial useful?

What is try

The try statement defines the code block to run (to try). The catch statement defines a code block to handle any error. The finally statement defines a code block to run regardless of the result. The throw statement defines a custom error. Both catch and finally are optional, but you must use one of them.

What is try

The primary method of handling exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any PHP exceptions that are thrown.

What is finally in PHP?

The finally keyword is used in try... finally and try... catch... finally structures to run a block of code whether or not an exception occurred.

Does finally execute after return PHP?

finally statement, the finally block still executes after the return statement. The result will be returned after the finally block is executed.