Bird
0
0

How do you correctly define a custom exception class named CustomError that inherits from PHP's built-in Exception class?

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

How do you correctly define a custom exception class named CustomError that inherits from PHP's built-in Exception class?

Aclass CustomError implements Exception {}
Bclass CustomError extends Exception {}
Cclass CustomError inherits Exception {}
Dclass CustomError extends Error {}
Step-by-Step Solution
Solution:
  1. Step 1: Understand inheritance syntax

    In PHP, classes extend other classes using the extends keyword.
  2. Step 2: Correct base class

    Custom exceptions must extend the built-in Exception class, not Error or implement it.
  3. Final Answer:

    class CustomError extends Exception {} -> Option B
  4. Quick Check:

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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes