PHP getPrevious() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Exception

Definition

In PHP, Exception::getPrevious() returns the previous Throwable that was passed when the exception was created. If there was no previous exception, it returns null.

Use it for exception chaining: catch a low-level error, throw a higher-level Exception that wraps it, then later walk getPrevious() to see the full cause. The method is final.

Syntax

Call getPrevious() with the values you need. Start with this line.

PHP
$cause = $e->getPrevious();
CallWhat it does
$e->getPrevious()Returns the previous Throwable, or null if none was set.
$e->getPrevious()?->getMessage()Safely reads the previous message when a previous exception exists.
while ($e = $e->getPrevious()) { ... }Walks the whole chain of nested exceptions.

Pass the previous Throwable as the third argument to Exception::__construct(). getPrevious() is final and returns null when no chain was created.

Library declarations
PHP
final public Exception::getPrevious(): ?Throwable

Parameters

ParameterDescription
—This method takes no parameters.

Return Value

Return ValueDescription
?ThrowableThe previous exception or error, or null if the constructor did not receive one.

PHP Examples

Example 1 — Wrap One Exception in Another

Catch a low-level Exception and rethrow a new one that keeps the original as previous.

PHP
<?php
try {
    try {
        throw new Exception("Connection refused");
    } catch (Exception $inner) {
        throw new Exception("Could not load profile", 0, $inner);
    }
} catch (Exception $e) {
    echo $e->getMessage(), PHP_EOL;
    echo $e->getPrevious()->getMessage(), PHP_EOL;
}

How It Works

  1. The inner throw creates the root cause.
  2. The outer throw wraps it with a friendlier message and passes $inner as previous.
  3. getMessage() shows the outer message.
  4. getPrevious()->getMessage() shows the original technical message.

Example 2 — No Previous Exception

See that getPrevious() returns null when you did not pass a third argument.

PHP
<?php
$e = new Exception("Simple error");
var_dump($e->getPrevious());

How It Works

  1. Only a message was passed to the constructor.
  2. There is no previous Throwable stored.
  3. getPrevious() therefore returns null.
  4. Use null-safe operators or an if check before reading nested details.

Example 3 — Walk the Full Chain

Loop with getPrevious() to print every message in a nested chain.

PHP
<?php
$a = new Exception("Level A");
$b = new Exception("Level B", 0, $a);
$c = new Exception("Level C", 0, $b);

$current = $c;
while ($current instanceof Throwable) {
    echo $current->getMessage(), PHP_EOL;
    $current = $current->getPrevious();
}

How It Works

  1. Three exceptions are linked from C to B to A.
  2. The loop starts at the outermost exception.
  3. Each step prints the message and moves to getPrevious().
  4. When getPrevious() returns null, the loop stops.

Frequently Asked Questions

It returns the Throwable that was passed as the previous argument when the exception was created, or null if there was none.
Pass it as the third argument: new Exception($message, $code, $previous).
Yes. The type is Throwable, so both Exception and Error objects can be chained.
The outer message can stay user-friendly while the previous exception keeps the technical detail for logs and debugging.
No. It is final on Exception.

Next: getCode()

Exception::getCode() returns the integer code stored in the exception. You set that code w.

getCode() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful