Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
PHP - Error and Exception Handling
Consider this code:
class MyException extends Exception {}

try {
    throw new MyException("Error!");
} catch (Exception $e) {
    echo "Caught: " . get_class($e);
}

What will be the output?
ACaught: Exception
BFatal error
CCaught: Error
DCaught: MyException
Step-by-Step Solution
Solution:
  1. Step 1: Understand inheritance in exceptions

    MyException extends Exception, so it is caught by catch(Exception $e).
  2. Step 2: Check get_class output

    The get_class($e) returns the actual class name of the caught exception, which is "MyException".
  3. Final Answer:

    Caught: MyException -> Option D
  4. Quick Check:

    get_class returns actual exception class name [OK]
Quick Trick: Catch parent class catches child exceptions too [OK]
Common Mistakes:
  • Assuming get_class returns parent class
  • Thinking catch(Exception) misses child exceptions
  • Expecting fatal error on subclass throw

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes