Bird
0
0

You want to throw a custom exception MyException with a message and code. Which code correctly throws it?

hard📝 Application Q8 of 15
PHP - Error and Exception Handling
You want to throw a custom exception MyException with a message and code. Which code correctly throws it?
Athrow new MyException("Failed", 404);
Bthrow MyException("Failed", 404);
Cthrow new Exception("Failed", 404);
Dthrow new MyException();
Step-by-Step Solution
Solution:
  1. Step 1: Understand custom exception throwing

    Custom exceptions are thrown like normal exceptions using new and can accept message and code parameters.
  2. Step 2: Check options

    throw new MyException("Failed", 404); correctly throws MyException with message and code. throw MyException("Failed", 404); misses new. throw new Exception("Failed", 404); throws base Exception, not custom. throw new MyException(); misses parameters.
  3. Final Answer:

    throw new MyException("Failed", 404); -> Option A
  4. Quick Check:

    Custom exceptions thrown with new and parameters [OK]
Quick Trick: Use 'throw new CustomException(message, code)' [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Using base Exception instead of custom
  • Missing constructor parameters

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes