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 Exception Object

Exception thrown in /hermes/bosnacweb05/bosnacweb05ap/b1920/ipg.philipramseyca/webdevelopment/webcourse/php/PHPExceptions4.php on line 31: [Code 1] Division by zero

<?php
// rhis code throws an exception
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception("Division by zero", 1);
  }
  return $dividend / $divisor;
}

// this code tries the code, catch the exception and identifies it and where it occurred
try {
  echo divide(5, 0);
} catch(Exception $ex) {
  $code = $ex->getCode();
  $message = $ex->getMessage();
  $file = $ex->getFile();
  $line = $ex->getLine();
  echo "Exception thrown in $file on line $line: [Code $code]
  $message";
}
?>