PHP try…catch…finally

Created with Sketch.

PHP try…catch…finally

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:

<?php

$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:

<?php

$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:

<?php

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.

<?php

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:

<?php
// ...
$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.

The try…finally statement

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

<?php
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.

Leave a Reply

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