0
0
PHPprogramming~10 mins

Multiple catch blocks in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple catch blocks
Try block starts
Code runs
Exception thrown?
NoTry block ends normally
Yes
Check first catch block type
Handle
No matching catch
Uncaught exception
End try-catch
The try block runs code that might throw exceptions. If an exception occurs, PHP checks each catch block in order to find one matching the exception type to handle it.
Execution Sample
PHP
<?php
try {
  throw new Exception("Error!");
} catch (TypeError $e) {
  echo "Type error caught";
} catch (Exception $e) {
  echo "Exception caught";
}
?>
This code throws an Exception, which is caught by the second catch block and prints a message.
Execution Table
StepActionException Thrown?Catch Block CheckedCatch Block Matches?Output
1Enter try blockNoN/AN/A
2Throw Exception("Error!")Yes: Exceptioncatch(TypeError)No
3Check next catch blockYes: Exceptioncatch(Exception)YesException caught
4Catch block executedHandledN/AN/AException caught
5Exit try-catchHandledN/AN/AException caught
💡 Exception caught by catch(Exception), so execution continues after try-catch.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
$eundefinedException object createdException object assigned in catchException object assigned in catch
Key Moments - 2 Insights
Why doesn't the first catch block handle the Exception?
Because the first catch block is for TypeError, but the thrown exception is Exception. The execution_table row 2 shows catch(TypeError) checked and does not match.
What happens if no catch block matches the exception?
The exception remains uncaught and causes a fatal error. The concept_flow shows that if no catch matches, the exception is uncaught.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, which catch block matches the exception?
Acatch(TypeError)
Bcatch(Exception)
CNo catch block matches
DBoth catch blocks match
💡 Hint
See execution_table row 3 where catch(Exception) matches the thrown Exception.
At which step does the exception get handled?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Check execution_table row 4 where catch block is executed and output is produced.
If the thrown exception was a TypeError instead, which catch block would handle it?
Acatch(Exception)
BNo catch block would handle it
Ccatch(TypeError)
DBoth catch blocks would handle it
💡 Hint
Refer to variable_tracker and execution_table row 2 where catch(TypeError) is checked first.
Concept Snapshot
try {
  // code that may throw
} catch (ExceptionType1 $e) {
  // handle ExceptionType1
} catch (ExceptionType2 $e) {
  // handle ExceptionType2
}

PHP checks catch blocks in order and runs the first matching one.
Full Transcript
This example shows how PHP handles multiple catch blocks. The try block runs code that throws an Exception. PHP checks each catch block in order. The first catch block is for TypeError, which does not match. The second catch block matches Exception, so it runs and prints 'Exception caught'. If no catch block matched, the exception would be uncaught and cause an error. Variables like $e hold the exception object inside the catch block. This flow helps handle different error types separately.