Bird
0
0

Which of the following is the correct way to declare a custom exception class named MyError in PHP?

easy📝 Syntax Q12 of 15
PHP - Error and Exception Handling

Which of the following is the correct way to declare a custom exception class named MyError in PHP?

?
Aclass MyError extends Error {}
Bclass MyError implements Exception {}
Cclass MyError extends Exception {}
Dclass MyError inherits Exception {}
Step-by-Step Solution
Solution:
  1. Step 1: Recall PHP syntax for custom exceptions

    Custom exceptions extend the built-in Exception class using the keyword extends.
  2. Step 2: Check each option

    class MyError extends Exception {} correctly uses extends Exception. class MyError implements Exception {} wrongly uses implements, which is for interfaces. class MyError extends Error {} extends Error, which is a different base class. class MyError inherits Exception {} uses invalid syntax inherits.
  3. Final Answer:

    class MyError extends Exception {} -> Option C
  4. Quick Check:

    Custom exception syntax = extends Exception [OK]
Quick Trick: Use 'extends Exception' to create custom exceptions [OK]
Common Mistakes:
  • Using 'implements' instead of 'extends'
  • Using 'inherits' keyword which is invalid
  • Extending Error instead of Exception

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes