Bird
0
0

Which of the following is the correct way to throw an exception in PHP?

easy📝 Syntax Q3 of 15
PHP - Error and Exception Handling
Which of the following is the correct way to throw an exception in PHP?
Araise Exception('Error message');
Bnew Exception('Error message');
Cthrow Exception('Error message');
Dthrow new Exception('Error message');
Step-by-Step Solution
Solution:
  1. Step 1: Recall PHP syntax for throwing exceptions

    In PHP, the keyword throw is used with new Exception() to create and throw an exception.
  2. Step 2: Check syntax correctness

    The correct syntax is throw new Exception('message'); which creates and throws the exception object.
  3. Final Answer:

    throw new Exception('Error message'); -> Option D
  4. Quick Check:

    Throw exception syntax = throw new Exception() [OK]
Quick Trick: Use 'throw new Exception' to raise exceptions [OK]
Common Mistakes:
  • Using 'raise' instead of 'throw'
  • Omitting 'new' keyword
  • Calling Exception without 'throw'

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes