Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP Exceptions

What is an Exception?

An exception is an object that represents an error or unexpected behavior in a PHP script.

Many PHP functions and classes throw exceptions.

User-defined functions and classes can also throw exceptions.

Exceptions provide a way to halt a function when it encounters data it cannot process.

Throwing an Exception

The throw statement enables a user-defined function or method to throw an exception. When an exception is thrown, any code following it will not be executed.

If an exception is not caught, a fatal error with an “Uncaught Exception” message will occur.

Let’s demonstrate throwing an exception without catching it:

Example

<?php
function divide($dividend, $divisor) {
  if($divisor == 0{
    throw new Exception(“Division by zero”);
  }
  return $dividend / $divisor;
}

echo divide(50);
?>

The result will appear as follows:

Fatal errorUncaught Exception: Division by zero in C:\webfolder\test.php:4
Stack trace: #0 C:\webfolder\test.php(9):
divide(5, 0) #1 {main} thrown in C:\webfolder\test.php on line 4

The try…catch Statement

To prevent the error shown in the example above, you can use the try…catch statement to catch exceptions and allow the process to continue.

Syntax

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

Example

Display a message when an exception is thrown:

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception(“Division by zero”);
  }
  return $dividend / $divisor;
}

try {
  echo divide(50);
catch(Exception $e) {
  echo “Unable to divide.”;
}
?>

The catch block specifies the type of exception to be caught and the variable name used to access the exception. In the example above, the exception type is Exception and the variable name is $e.

The try…catch…finally Statement

The try…catch…finally statement can be used to handle exceptions. Code within the finally block will always execute, whether or not an exception was caught. If a finally block is used, the catch block becomes optional.

Syntax

try {
  code that can throw exceptions
catch(Exception $e) {
  code that runs when an exception is caught
finally {
  code that always runs regardless of whether an exception was caught
}

Example

Display a message when an exception is thrown, and then indicate that the process has concluded.

<?php
function divide($dividend, $divisor) {
  if($divisor == 0) {
    throw new Exception(“Division by zero”);
  }
  return $dividend / $divisor;
}

try {
  echo divide(50);
catch(Exception $e) {
  echo “Unable to divide. “;
} finally {
  echo “Process complete.”;
}
?>

Example

Output a string regardless of whether an exception was caught.

<?php
function divide($dividend, $divisor) {
  if($divisor == 0{
    throw new Exception(“Division by zero”);
  }
  return $dividend / $divisor;
}

try {
  echo divide(50);
} finally {
  echo “Process complete.”;
}
?>

The Exception Object

The Exception object provides details about the error or unexpected behavior encountered by the function.

Syntax

new Exception(message, code, previous)

Parameter Values

Parameter

Description

message

Optional: A string explaining the reason the exception was thrown

code

Optional: An integer used to uniquely identify this exception among others of the same type

previous

Optional: If this exception was thrown within a catch block of another exception, it is recommended to pass the previous exception into this parameter.

Methods

When catching an exception, the following table lists some methods you can use to obtain information about the exception:

Method

Description

getMessage()

Returns a string explaining the reason for the exception being thrown

getPrevious()

If this exception was caused by another exception, this method returns the previous exception; otherwise, it returns null.

getCode()

Returns the exception code

getFile()

Returns the full path of the file where the exception was thrown

getLine()

Returns the line number of the code that threw the exception

Example

Display information about an exception that was thrown:

<?php
function divide($dividend, $divisor) {
  if($divisor == 0{
    throw new Exception(“Division by zero”1);
  }
  return $dividend / $divisor;
}

try {
  echo divide(50);
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”
;
}
?>