How do you throw an exception in PHP?

Question

Grade: Education Subject: Support
How do you throw an exception in PHP?
Asked by:
37 Viewed 37 Answers

Answer (37)

Best Answer
(381)
You throw an exception in PHP using the `throw` keyword followed by an instance of an exception class. For example: `throw new Exception('This is an error message.');`. You can also create custom exception classes by extending the `Exception` class to represent specific error conditions in your application. Always include a descriptive error message when throwing an exception.