Recall & Review
beginner
What is the purpose of multiple catch blocks in PHP?
Multiple catch blocks allow you to handle different types of exceptions separately, providing specific responses for each exception type.
Click to reveal answer
beginner
How do you write multiple catch blocks in PHP?
You write multiple catch blocks by following a try block with several catch blocks, each specifying a different exception type to handle.
Click to reveal answer
intermediate
What happens if an exception is thrown that does not match any catch block?
If no catch block matches the thrown exception, the exception will remain unhandled and cause a fatal error unless caught elsewhere.
Click to reveal answer
intermediate
Can you catch multiple exception types in a single catch block in PHP?
Yes, since PHP 7.1, you can catch multiple exception types in one catch block by separating them with a pipe (|) symbol.
Click to reveal answer
beginner
Example: What will this code output?
try {
throw new InvalidArgumentException('Invalid argument');
} catch (RuntimeException $e) {
echo 'Runtime error';
} catch (InvalidArgumentException $e) {
echo 'Invalid argument error';
}The output will be: Invalid argument error because the thrown exception matches the second catch block.
Click to reveal answer
What keyword starts the block where exceptions are caught in PHP?
✗ Incorrect
The catch keyword is used to define blocks that handle exceptions thrown in the preceding try block.
If an exception matches multiple catch blocks, which one runs?
✗ Incorrect
Only the first catch block that matches the exception type runs.
How do you catch multiple exception types in one catch block in PHP 8.0+?
✗ Incorrect
You separate exception types with a pipe (|) symbol inside a single catch block.
What happens if no catch block matches the thrown exception?
✗ Incorrect
If no catch block matches, the exception is unhandled and causes a fatal error.
Which block always runs after try and catch blocks, regardless of exceptions?
✗ Incorrect
The finally block runs after try and catch blocks no matter what.
Explain how multiple catch blocks work in PHP and why they are useful.
Think about how you handle different problems separately in real life.
You got /4 concepts.
Describe how to catch multiple exception types in one catch block and when this might be helpful.
Imagine grouping similar problems to handle them together.
You got /4 concepts.