Bird
0
0

Which of the following PHP code snippets correctly throws an exception with the message "Access denied"?

easy📝 Syntax Q3 of 15
PHP - Error and Exception Handling
Which of the following PHP code snippets correctly throws an exception with the message "Access denied"?
Athrow Exception("Access denied");
Bthrow new Exception("Access denied");
Cthrow new Exception["Access denied"];
Dthrow Exception->new("Access denied");
Step-by-Step Solution
Solution:
  1. Step 1: Syntax for throwing exceptions

    In PHP, to throw an exception, you must create a new Exception object using new Exception() and then use throw.
  2. Step 2: Check each option

    throw new Exception("Access denied"); correctly uses throw new Exception("Access denied"). Options B, C, and D have syntax errors.
  3. Final Answer:

    throw new Exception("Access denied"); -> Option B
  4. Quick Check:

    Use 'throw new Exception()' syntax [OK]
Quick Trick: Always use 'throw new Exception()' to throw exceptions [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Using square brackets instead of parentheses
  • Incorrect method call syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes