Bird
0
0

Why is it recommended to catch Throwable instead of just Exception in PHP 7+?

hard📝 Conceptual Q10 of 15
PHP - Error and Exception Handling
Why is it recommended to catch Throwable instead of just Exception in PHP 7+?
ABecause <code>Throwable</code> only catches fatal errors.
BBecause <code>Throwable</code> includes both <code>Error</code> and <code>Exception</code>, allowing broader error handling.
CBecause <code>Exception</code> is deprecated in PHP 7+.
DBecause <code>Exception</code> cannot be caught in PHP 7+.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Throwable interface role

    In PHP 7+, Throwable is the base interface for all errors and exceptions.
  2. Step 2: Compare Exception and Throwable

    Catching Throwable allows handling both Error and Exception types, providing more comprehensive error management.
  3. Final Answer:

    Because Throwable includes both Error and Exception, allowing broader error handling. -> Option B
  4. Quick Check:

    Throwable catch = broader error handling [OK]
Quick Trick: Catch Throwable to handle all errors and exceptions [OK]
Common Mistakes:
  • Thinking Exception is deprecated
  • Believing Throwable catches only fatal errors
  • Assuming Exception cannot be caught

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes