Challenge - 5 Problems
PHP Multiple Catch Blocks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of multiple catch blocks with different exceptions
What will be the output of this PHP code with multiple catch blocks?
PHP
<?php try { throw new InvalidArgumentException("Invalid argument!"); } catch (RuntimeException $e) { echo "Caught RuntimeException"; } catch (InvalidArgumentException $e) { echo "Caught InvalidArgumentException"; } catch (Exception $e) { echo "Caught Exception"; } ?>
Attempts:
2 left
💡 Hint
The thrown exception matches the first catch block that fits its type.
✗ Incorrect
The thrown exception is InvalidArgumentException. The first catch block for RuntimeException does not match. The second catch block matches exactly, so it runs and prints 'Caught InvalidArgumentException'.
❓ Predict Output
intermediate2:00remaining
Which catch block handles the exception?
Given this PHP code, which catch block will handle the thrown exception?
PHP
<?php try { throw new OutOfRangeException("Out of bounds!"); } catch (InvalidArgumentException $e) { echo "Caught InvalidArgumentException"; } catch (OutOfRangeException $e) { echo "Caught OutOfRangeException"; } catch (Exception $e) { echo "Caught Exception"; } ?>
Attempts:
2 left
💡 Hint
The exception type matches one of the catch blocks exactly.
✗ Incorrect
The thrown exception is OutOfRangeException. The first catch block is for InvalidArgumentException, which does not match. The second catch block matches exactly, so it runs and prints 'Caught OutOfRangeException'.
❓ Predict Output
advanced2:00remaining
Output when exception matches parent class catch block
What will this PHP code output when an exception is thrown that matches a parent class catch block but not the earlier catch blocks?
PHP
<?php class MyException extends Exception {} try { throw new MyException("My exception occurred"); } catch (InvalidArgumentException $e) { echo "Caught InvalidArgumentException"; } catch (Exception $e) { echo "Caught Exception"; } catch (MyException $e) { echo "Caught MyException"; } ?>
Attempts:
2 left
💡 Hint
Catch blocks are checked in order; parent class catch blocks can catch child exceptions.
✗ Incorrect
The thrown exception is MyException, which extends Exception. The first catch block is for InvalidArgumentException, no match. The second catch block is for Exception, which is a parent class of MyException, so it catches the exception and prints 'Caught Exception'. The third catch block is never reached.
❓ Predict Output
advanced2:00remaining
Output when multiple catch blocks could match
What will this PHP code output when an exception is thrown that matches multiple catch blocks due to inheritance?
PHP
<?php class BaseException extends Exception {} class ChildException extends BaseException {} try { throw new ChildException("Child exception"); } catch (BaseException $e) { echo "Caught BaseException"; } catch (Exception $e) { echo "Caught Exception"; } catch (ChildException $e) { echo "Caught ChildException"; } ?>
Attempts:
2 left
💡 Hint
Catch blocks are checked in order; the first matching catch block runs.
✗ Incorrect
The thrown exception is ChildException, which extends BaseException. The first catch block is for BaseException, which matches because ChildException is a subclass. So it prints 'Caught BaseException'. The other catch blocks are not reached.
❓ Predict Output
expert3:00remaining
Output when rethrowing exception in multiple catch blocks
What will be the output of this PHP code that rethrows an exception in one catch block and catches it again in another?
PHP
<?php try { try { throw new Exception("Original exception"); } catch (Exception $e) { echo "First catch: " . $e->getMessage() . "\n"; throw $e; } } catch (Exception $e) { echo "Second catch: " . $e->getMessage() . "\n"; } ?>
Attempts:
2 left
💡 Hint
An exception rethrown inside a catch block can be caught by an outer catch block.
✗ Incorrect
The inner try throws an exception caught by the inner catch, which prints 'First catch: Original exception' and rethrows it. The outer catch then catches the rethrown exception and prints 'Second catch: Original exception'.