0
0
PHPprogramming~20 mins

Multiple catch blocks in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP Multiple Catch Blocks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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";
}
?>
ACaught RuntimeException
BCaught Exception
CNo output, program crashes
DCaught InvalidArgumentException
Attempts:
2 left
💡 Hint
The thrown exception matches the first catch block that fits its type.
Predict Output
intermediate
2: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";
}
?>
ACaught OutOfRangeException
BCaught InvalidArgumentException
CCaught Exception
DNo output, program crashes
Attempts:
2 left
💡 Hint
The exception type matches one of the catch blocks exactly.
Predict Output
advanced
2: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";
}
?>
ACaught Exception
BCaught InvalidArgumentException
CCaught MyException
DNo output, program crashes
Attempts:
2 left
💡 Hint
Catch blocks are checked in order; parent class catch blocks can catch child exceptions.
Predict Output
advanced
2: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";
}
?>
ACaught ChildException
BCaught Exception
CCaught BaseException
DNo output, program crashes
Attempts:
2 left
💡 Hint
Catch blocks are checked in order; the first matching catch block runs.
Predict Output
expert
3: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";
}
?>
A
First catch: Original exception
B
First catch: Original exception
Second catch: Original exception
C
Second catch: Original exception
DNo output, program crashes
Attempts:
2 left
💡 Hint
An exception rethrown inside a catch block can be caught by an outer catch block.