Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q13 of 15
PHP - Error and Exception Handling
What will be the output of this PHP code?
try {
  throw new Exception('Error happened');
  echo 'No error';
} catch (Exception $e) {
  echo 'Caught: ' . $e->getMessage();
}
ANo error
BFatal error: uncaught exception
CCaught: No error
DCaught: Error happened
Step-by-Step Solution
Solution:
  1. Step 1: Understand the throw statement

    The code throws an Exception with message 'Error happened', so normal flow stops at throw.
  2. Step 2: Catch block executes and prints message

    The catch block catches the exception and echoes 'Caught: ' plus the exception message.
  3. Final Answer:

    Caught: Error happened -> Option D
  4. Quick Check:

    Exception thrown and caught = 'Caught: Error happened' [OK]
Quick Trick: Exception stops try block; catch prints message [OK]
Common Mistakes:
  • Expecting code after throw to run
  • Confusing exception message with echo text
  • Thinking uncaught error occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes