3v4l.org

run code in 300+ PHP versions simultaneously
<?php define('MAX_ATTEMPTS', 5); class NetworkException extends Exception { } class FetchDataException extends Exception { private $surpressed = []; public function addSuppressed(Throwable $e): void { $this->surpressed[] = $e; } public function getSuppressed(): array { return $this->surpressed; } } function fetchDataOverNetwork() { $networkExceptions = []; for ($i = 0; $i < MAX_ATTEMPTS; $i++) { try { // Some operation that may throw throw new NetworkException(); } catch (NetworkException $ne) { $networkExceptions[] = $ne; } } $fdException = new FetchDataException("Failed to get data"); foreach ($networkExceptions as $networkException) { $fdException->addSuppressed($networkException); } throw $fdException; } try { fetchDataOverNetwork(); } catch(FetchDataException $fde) { var_dump($fde->getSuppressed()); }
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
array(5) { [0]=> object(NetworkException)#1 (7) { ["message":protected]=> string(0) "" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/cboHF" ["line":protected]=> int(26) ["trace":"Exception":private]=> array(1) { [0]=> array(4) { ["file"]=> string(9) "/in/cboHF" ["line"]=> int(43) ["function"]=> string(20) "fetchDataOverNetwork" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL } [1]=> object(NetworkException)#2 (7) { ["message":protected]=> string(0) "" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/cboHF" ["line":protected]=> int(26) ["trace":"Exception":private]=> array(1) { [0]=> array(4) { ["file"]=> string(9) "/in/cboHF" ["line"]=> int(43) ["function"]=> string(20) "fetchDataOverNetwork" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL } [2]=> object(NetworkException)#3 (7) { ["message":protected]=> string(0) "" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/cboHF" ["line":protected]=> int(26) ["trace":"Exception":private]=> array(1) { [0]=> array(4) { ["file"]=> string(9) "/in/cboHF" ["line"]=> int(43) ["function"]=> string(20) "fetchDataOverNetwork" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL } [3]=> object(NetworkException)#4 (7) { ["message":protected]=> string(0) "" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/cboHF" ["line":protected]=> int(26) ["trace":"Exception":private]=> array(1) { [0]=> array(4) { ["file"]=> string(9) "/in/cboHF" ["line"]=> int(43) ["function"]=> string(20) "fetchDataOverNetwork" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL } [4]=> object(NetworkException)#5 (7) { ["message":protected]=> string(0) "" ["string":"Exception":private]=> string(0) "" ["code":protected]=> int(0) ["file":protected]=> string(9) "/in/cboHF" ["line":protected]=> int(26) ["trace":"Exception":private]=> array(1) { [0]=> array(4) { ["file"]=> string(9) "/in/cboHF" ["line"]=> int(43) ["function"]=> string(20) "fetchDataOverNetwork" ["args"]=> array(0) { } } } ["previous":"Exception":private]=> NULL } }

preferences:
158.56 ms | 418 KiB | 5 Q