Bird
0
0

What will this code output?

hard📝 Application Q9 of 15
PHP - Error and Exception Handling

What will this code output?

try {
    echo "X ";
    throw new Exception("Error");
} catch (Exception $e) {
    echo "Y ";
    throw new Exception("New Error");
} finally {
    echo "Z ";
}
AX Y Z and then fatal error
BX Y Z
CX Z
DX Y
Step-by-Step Solution
Solution:
  1. Step 1: Execute try block

    Prints "X ", then throws Exception.
  2. Step 2: catch block runs, prints "Y ", then throws new Exception

    This new exception is not caught inside this block.
  3. Step 3: finally block runs, prints "Z "

    After finally, uncaught exception causes fatal error.
  4. Final Answer:

    Output is "X Y Z " followed by fatal error. -> Option A
  5. Quick Check:

    New exception thrown in catch triggers fatal error after finally [OK]
Quick Trick: Exception thrown in catch triggers fatal error after finally [OK]
Common Mistakes:
  • Assuming new exception is caught automatically
  • Ignoring finally output
  • Thinking code stops before finally

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes