Content · Web Development · PHP

PHP Exception::getPrevious() Method: Usage and Examples

PHP Exception::getPrevious() Method: Usage and Examples: Returns the previous Throwable in a causal exception chain, or null. See three examples and important limitations.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

The PHP Exception::getPrevious API performs a focused operation on an object or value. The examples below show its effect on actual data.

Returns the previous Throwable in a causal exception chain, or null.

What Is Exception::getPrevious in PHP?


Returns the previous Throwable in a causal exception chain, or null.

Syntax:

Exception::getPrevious(): ?Throwable

This operation has no parameters.

Return type declared by PHP: ?Throwable. The value or side effect is demonstrated in the examples below.

Example 1: Basic Usage


This example uses a small input to make the main behavior visible.

<?php

$originalError = new Exception("Unable to process order", 17, new RuntimeException("Underlying failure"));
echo $originalError->getPrevious()?->getMessage();

Output:

Underlying failure

Returns the previous Throwable in a causal exception chain, or null.

Example 2: A Different Input


A different set of values shows the same operation in another situation.

<?php

$originalError = new Exception("Unable to read configuration", 42, new RuntimeException("Missing file"));
echo $originalError->getPrevious()?->getMessage();

Output:

Missing file

The previous exception can be null; always use a null check before reading its details.

Practical Example: Applying the Feature


This example shows an application-style use of the feature, with its surrounding setup visible.

<?php

$originalError = new Exception("Unexpected response", 503, new RuntimeException("Timeout"));
echo $originalError->getPrevious()?->getMessage();

Output:

Timeout

The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.

Important Behavior and Edge Cases


  • The previous exception can be null; always use a null check before reading its details.
  • Returns the previous Throwable in a causal exception chain, or null.
  • Observe differences in empty inputs, invalid states and optional extension availability where relevant.

Comparison With Related Approaches


ApproachImportant Difference
Exception::getPreviousReturns the previous Throwable in a causal exception chain, or null.
ErrorException is commonly thrown by applications, while Error represents PHP runtime issues; both expose Throwable diagnostic methods.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of Exception::getPreviousThe previous exception can be null; always use a null check before reading its details.
Expecting a different type of resultRead the declared return type and distinguish returned values from changes to the source object.
Assuming a related API works identicallyException is commonly thrown by applications, while Error represents PHP runtime issues; both expose Throwable diagnostic methods.

Key Takeaways


  • Returns the previous Throwable in a causal exception chain, or null.
  • The previous exception can be null; always use a null check before reading its details.
  • Use the example that matches the values, runtime version and dependencies in your application.

Exception::getPrevious is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.

Share this resourceSend this page to someone who may find it useful.