Code has been added to clipboard!

Triggering PHP Exception

  //creating a function containing a potential exception
  function checkNumber($number) {
    if($number > 1) {
      throw new Exception('The value has to be 1 or lower');
    return true;

  //triggering the exception inside a try()
  try {
    //In case the exception gets thrown, this is the message that is shown.
    echo 'The value is 1 or lower';
  } catch(Exception $e) {
    //catching the exception
    echo 'Message: ' .$e->getMessage();