A tray inside a tray
save() catches a low-level failure and throws a clearer one, keeping the original.
<?phpclass SaveFailed extends Exception {}function write(): void { throw new RuntimeException('disk full');}function save(): void { try { write(); } catch (RuntimeException $e) { echo "save logs: ", $e->getMessage(), "\n"; throw new SaveFailed('could not save', 0, $e); }}try { save();} catch (SaveFailed $e) { echo $e->getMessage(), "\n"; echo "because: ", $e->getPrevious()->getMessage(), "\n";}1. Predict the output
Tap the lines in the order you think they print.
- 1…
- 2…
- 3…