Back to questions
JuniorTheoryCommon
What does the program print?
<?php
class OutOfStockException extends RuntimeException {}
try {
echo 'start ';
throw new OutOfStockException('need 3');
} catch (RuntimeException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}start OutOfStockException: need 3
- Expecting
catch (RuntimeException $e)to miss a subclass: acatchaccepts the class it names and every class that extends it, soOutOfStockExceptionis caught. - Expecting
get_class($e)to give the class written incatch: it gives the class of the object that was thrown, hereOutOfStockException.
- Why declare
OutOfStockExceptionat all instead of throwing a plainRuntimeException? - In which order should two
catchblocks forOutOfStockExceptionandRuntimeExceptiongo?