try { // Some code that may throw an exception } catch (Exception $e) { $previous = $e->getPrevious(); if ($previous !== null) { // Handle the previous exception } }
class MyException extends Exception { public function __construct($message, $code, $previous = null) { parent::__construct($message, $code, $previous); } } try { // Some code that may throw an exception } catch (MyException $e) { $previous = $e->getPrevious(); if ($previous !== null) { // Handle the previous exception } }This example demonstrates how to create a custom Exception class that can also capture and handle a previous exception. Package/Library: This is a built-in PHP feature and does not require any third-party package or library.