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()); }

preferences:
20.1 ms | 405 KiB | 5 Q