PHP

PHP Menu

PHP

Exceptions - PHP Advance

Exceptions in PHP is a way handling errors. With the use of exceptions, you can have more control and flexibility in reporting errors.

The throw statement

The throw statement defines a function or method to throw an exception. consider the following example:

<?php
function divide($x, $y) { if($y == 0) { throw new Exception("Cannot divide by zero"); } return $x / $y; }
echo divide(2, 0);

The try...catch Statement

The example above displays a long string describing the error. If you want to avoid this, use the try...catch statement instead. The basic syntax for this statement is:

try {
  // code that can throw exceptions
} 
catch(Exception $e) {
  // code that runs when an exception is caught
}

Using the try...catch statement on the previous example would look like:

<?php
function divide($x, $y) { if($y == 0) { throw new Exception("Cannot divide by zero"); } return $x / $y; }
// using the try..catch statement try { echo divide(2, 0); echo divide(9,0); } catch(Exception $e) { echo "Error: Division by zero."; }

The try...catch...finally statement

The try...catch statement can be extended to use the finally block. The try block tries to execute the code. If the try block throws an error, the catch block is then executed. The finally block is executed regardless if there was an exception. This block (finally) is optional.

<?php
function divide($x, $y) { if ($y == 0) { throw new Exception("Cannot divide by zero"); } return $x / $y; }
// try block try { echo divide(2, 0); echo divide(9,0); } // catch block catch(Exception $e) { echo "Error: Division by zero."; } // finally block finally { echo "<br>"; echo "Calculation complete."; }

Exercise

The script for validating whether the value of the variable is a valid integer, is given below. Create a function, myFunction, using the given code. Using your created function, verify if 2.5 is a valid integer. Print Validation Complete after validating, using the finally block.

<?php
$myint = 0;
if (filter_var($myint, FILTER_VALIDATE_INT) === 0 || ! filter_var($myint, FILTER_VALIDATE_INT) === false) { echo("$myint is a valid integer"); } else { echo("$myint is not a valid integer"); }
<?php
function myFunction($myint) { if (filter_var($myint, FILTER_VALIDATE_INT) === 0 || ! filter_var($myint, FILTER_VALIDATE_INT) === false) { echo "$myint is a valid integer"; } else { echo "$myint is not a valid integer"; } } try { myFunction(2.5); } catch(Exception $e) { echo "Try again."; } finally{ echo "<br>"; echo "Validation Complete"; }
{ "test_output_contains":[ { "expected":"2.5 is not a valid integer", "error_message":"Have you outputted <code>2.5 is not a valid integer<\/code>?" }, { "expected":"2.5 is not a valid integer", "error_message":"Have you outputted <code>Validation Complete<\/code>?" } ], "test_variable_exists": { "object":"$myint", "error_message":"Have you declared <code>$myint<\/code>?" }, "test_function_exists": { "object":"myFunction", "error_message":"Did you create <code>myFunction<\/code>?" }, "success_message":"Good job!", "error_message":"There is something wrong on your code." }

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods