This site is mobile accessible. Press the "Tap Here" button to use a different font-size.


Smartphone icons created by Freepik - Flaticon

PHP Exceptions

The try...catch...finally Statement

Unable to divide. Process complete.

<?php
// this throws an exception and will result in a fatal error if it is not caught
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero");
  }
  return $dividend / $divisor;
}

//try ... catch tries the code and catches the error and finally displays "Process complete" mrssage
try {
  echo divide(5, 0);
} catch(Exception $e) {
  echo "Unable to divide. ";
} finally {
  echo "Process complete.";
}
?>