CopyPastehas never been so tasty!

How to throw exception in PHP?

by anonymous

  • 0
  • 0
  • 0

In this simple article you will learn that how we throw exception in PHP.


Exceptions are the abnormal behavior of your code that was occurring unexpectedly. Exception can be raised due to the system or the program itself.

Throw Exceptions in PHP:

To throw exception in php we use throw exception function. It sends a parameter error variable that was initialized with some string message. All the code is written in try catch block. In try block we throw exception and in catch block we catch the exception.


try {
$error = 'Always throw this error';
throw new Exception($error);
// Code following an exception is not executed.
echo 'Never executed';
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
// Continue execution
echo 'Hello World';

Above is a simple code to throw exceptions in php.

This simple article tells that how we can throw exception in php.

Add A Comment: