Multiple catch blocks let you handle different errors in different ways. This helps your program respond correctly to each problem.
Multiple catch blocks in PHP
<?php try { // code that may throw exceptions } catch (ExceptionType1 $e) { // handle ExceptionType1 } catch (ExceptionType2 $e) { // handle ExceptionType2 } catch (Exception $e) { // handle any other exceptions } ?>
Each catch block handles one specific type of exception.
The catch blocks are checked in order, so put more specific exceptions first.
<?php try { throw new InvalidArgumentException("Invalid argument!"); } catch (InvalidArgumentException $e) { echo "Caught invalid argument: " . $e->getMessage(); } catch (Exception $e) { echo "Caught general exception: " . $e->getMessage(); } ?>
<?php try { throw new RuntimeException("Runtime error!"); } catch (InvalidArgumentException $e) { echo "Caught invalid argument."; } catch (RuntimeException $e) { echo "Caught runtime exception."; } ?>
This program throws different exceptions based on input and catches them in separate catch blocks, printing a message for each.
<?php function testException($type) { try { if ($type === 'invalid') { throw new InvalidArgumentException("Invalid argument provided."); } elseif ($type === 'runtime') { throw new RuntimeException("Runtime problem occurred."); } else { throw new Exception("General error."); } } catch (InvalidArgumentException $e) { echo "Caught InvalidArgumentException: " . $e->getMessage() . "\n"; } catch (RuntimeException $e) { echo "Caught RuntimeException: " . $e->getMessage() . "\n"; } catch (Exception $e) { echo "Caught Exception: " . $e->getMessage() . "\n"; } } testException('invalid'); testException('runtime'); testException('other'); ?>
Always order catch blocks from most specific to most general exceptions.
If no catch block matches, the exception will cause a fatal error.
Multiple catch blocks let you handle different errors in different ways.
Put more specific exceptions first, then more general ones.
This helps your program respond clearly and correctly to problems.
Practice
What is the main purpose of using multiple catch blocks in PHP?
Solution
Step 1: Understand the role of catch blocks
Catch blocks are used to handle exceptions that occur in the try block.Step 2: Recognize the benefit of multiple catch blocks
Multiple catch blocks allow handling different exception types in different ways, improving error management.Final Answer:
To handle different types of exceptions separately -> Option AQuick Check:
Multiple catch blocks = different exception handling [OK]
- Thinking multiple catch blocks run in parallel
- Believing catch blocks catch all errors without type
- Assuming multiple catch blocks improve speed
Which of the following is the correct syntax to catch multiple exceptions in PHP?
try {
// code
} catch (ExceptionType1 $e) {
// handle
} catch (ExceptionType2 $e) {
// handle
}Solution
Step 1: Review PHP multiple catch block syntax
PHP uses separate catch blocks for each exception type, each with its own parentheses.Step 2: Identify the correct syntax
try { } catch (ExceptionType1 $e) { } catch (ExceptionType2 $e) { } shows two separate catch blocks, which is the correct way to catch multiple exceptions.Final Answer:
try { } catch (ExceptionType1 $e) { } catch (ExceptionType2 $e) { } -> Option CQuick Check:
Separate catch blocks = correct syntax [OK]
- Trying to catch multiple exceptions in one catch parentheses
- Using commas or pipes inside catch parentheses incorrectly
- Missing parentheses around exception type and variable
What will be the output of the following PHP code?
try {
throw new InvalidArgumentException("Invalid argument!");
} catch (RuntimeException $e) {
echo "Runtime error caught";
} catch (InvalidArgumentException $e) {
echo "Invalid argument caught";
}Solution
Step 1: Identify the thrown exception type
The code throws an InvalidArgumentException.Step 2: Match the thrown exception with catch blocks
The first catch block is for RuntimeException, which does not match. The second catch block matches InvalidArgumentException, so it runs.Final Answer:
Invalid argument caught -> Option AQuick Check:
Exception type matches second catch block [OK]
- Assuming first catch block always runs
- Confusing exception types
- Expecting no output if first catch fails
Find the error in this PHP code snippet:
try {
// some code
} catch (Exception $e) {
echo "General exception";
} catch (InvalidArgumentException $e) {
echo "Invalid argument";
}Solution
Step 1: Understand catch block order importance
More specific exceptions must be caught before more general ones to avoid unreachable code.Step 2: Identify the order problem
The general Exception catch block is first, so the InvalidArgumentException catch block is unreachable and causes an error.Final Answer:
The catch blocks are in wrong order; specific exceptions must come first -> Option DQuick Check:
Specific catch before general catch [OK]
- Putting general catch before specific catch
- Ignoring unreachable catch block warnings
- Assuming order does not matter
Consider this PHP code:
try {
if (rand(0,1)) {
throw new RuntimeException("Runtime issue");
} else {
throw new InvalidArgumentException("Invalid argument");
}
} catch (InvalidArgumentException | RuntimeException $e) {
echo "Caught: " . $e->getMessage();
} catch (Exception $e) {
echo "General exception";
}What will this code output?
Solution
Step 1: Understand combined catch block syntax
PHP 7.1+ allows catching multiple exceptions in one catch using the pipe (|) symbol.Step 2: Analyze thrown exceptions and matching catch
Either RuntimeException or InvalidArgumentException is thrown randomly. Both are caught by the combined catch block, which prints the message.Step 3: Confirm no syntax error
The combined catch syntax is valid, so no syntax error occurs.Final Answer:
Caught: Runtime issue or Caught: Invalid argument -> Option BQuick Check:
Combined catch handles both exceptions [OK]
- Thinking multiple exceptions in one catch cause syntax error
- Expecting general exception catch to run first
- Ignoring random exception thrown
Ready to prove it?
Pass the test and this pattern moves unknown
