Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Error and Exception Handling
What will be the output of this PHP code?
try {
  throw new Exception('Oops!');
} catch (Exception $e) {
  echo 'Caught: ' . $e->getMessage();
}
AOops!
BNo output
CException thrown
DCaught: Oops!
Step-by-Step Solution
Solution:
  1. Step 1: Understand the throw statement

    The code throws an Exception with the message 'Oops!'.
  2. Step 2: Catch block processes the exception

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

    Caught: Oops! -> Option D
  4. Quick Check:

    Exception caught message = Caught: Oops! [OK]
Quick Trick: Catch blocks display exception messages with custom text [OK]
Common Mistakes:
  • Expecting only the message without 'Caught:' prefix
  • Thinking exception is printed automatically
  • Assuming no output if exception is thrown

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes