Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

try-catch
php
communitycreator

What is the finally block in try-catch in PHP?

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Exception

An exception can be defined as a discontinuity in the normal flow of program execution. It includes errors that cause a complete disruption in the execution of code.

Exception handling

Exception handling allows the program to respond appropriately when an exception occurs. In the event of an exception, exception handling makes the program respond to its actual cause and its place of occurrence.

In this shot, we'll learn how to use the finally block in try-catch exception handling.

try-catch

The try-catch statement is used to check for errors in code. It guides the program to give specific error messages (in the catch block) when an error occurs in the try block.

finally

Code in the finally block will run irrespective of the block it is in. In try-catch, the finally block is used to execute a code block whether or not an error occurs. It can be used to continue the flow of the application even though an error occurs when the same code is placed in the catch block.

Note: Codes in the finally block will be executed even though code in the try block has an error that the catch block needs to display.

Syntax

(try
(//Protected code)
catch Exception e1)
(//Catch block)
(finally
//Cleanup code)
Syntax for try-catch with finally block

Example

Let's look at the code below:

<?php
function func($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
}
try {
func('') . "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
} finally {
echo "First finally.\n";
}
?>
Try-catch with finally block example

Explanation

In the code above:

  • Line 4: We define an exception handling scenario under which the program will throw an exception if the number supplied to the func function is empty.
  • Line 9: We start our try block.
  • Line 10: We call the func function and pass an empty string to it. This will throw an error.
  • Line 11: We use the catch block to catch and echo the error thrown.
  • Line 13: We use the finally block to echo a message (First finally.\n).

Note: The finally block can be used to execute any code we want, not just echo a string to the screen.

RELATED TAGS

try-catch
php
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring