PHP finally Keyword

❮ PHP Keywords

Example

Run some code regardless of whether an exception was thrown:

<?php
echo "Starting the process.";
try {
  // Select randomly between 0 and 1, throw an exception if 1 is selected.
  $random = rand(0, 1);
  if($random == 1) {
    throw new Exception("Exception");
  }
} finally {
  echo "Process complete";
}
?>
Try it Yourself »

Definition and Usage

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.


Related Pages

The throw keyword.

The catch keyword.

The finally keyword.

Read more about exceptions in our PHP Exceptions Tutorial.


❮ PHP Keywords
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.