0
0
PHPprogramming~5 mins

Multiple catch blocks in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Acatch
Btry
Cthrow
Dfinally
If an exception matches multiple catch blocks, which one runs?
AThe first matching catch block
BThe last matching catch block
CAll matching catch blocks
DNone, it causes an error
How do you catch multiple exception types in one catch block in PHP 8.0+?
AUse an array of types
BUse multiple catch blocks
CSeparate types with commas
DSeparate types with a pipe (|)
What happens if no catch block matches the thrown exception?
AThe program continues normally
BThe exception is ignored
CA fatal error occurs
DThe exception is automatically caught
Which block always runs after try and catch blocks, regardless of exceptions?
Acatch
Bfinally
Cthrow
Delse
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.