PHP __construct() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Exception

Definition

In PHP, Exception::__construct() creates a new Exception object. You pass a message, an optional numeric code, and an optional previous Throwable so you can chain errors.

You rarely call __construct() by name. Instead you write throw new Exception("...") or new MyException(...). If you extend Exception and write your own constructor, call parent::__construct() so message, code, and previous are stored correctly.

Syntax

Call __construct() with the values you need. Start with this line.

PHP
$e = new Exception("Something went wrong", 100);
CallWhat it does
new Exception("Disk full")Creates an exception with a message and the default code 0.
new Exception("Not found", 404)Sets both a message and a custom integer code.
new Exception("Wrap", 1, $prev)Chains a previous Throwable so getPrevious() can return it.

new Exception(...) calls __construct() for you. Subclasses that redefine the constructor should call parent::__construct($message, $code, $previous). The message is not binary-safe.

Library declarations
PHP
public Exception::__construct(string $message = "", int $code = 0, ?Throwable $previous = null)

Parameters

ParameterDescription
messageOptional. The text that describes what went wrong. The default is an empty string.
codeOptional. An integer you choose for your own error categories. The default is 0.
previousOptional. Another Throwable that caused this one. Use it for exception chaining. The default is null.

Return Value

Return ValueDescription
ExceptionA new Exception instance. Throw it with throw, or catch it later and read getMessage(), getCode(), and the other getters.

PHP Examples

Example 1 — Create and Throw an Exception

Build a simple Exception with a message and catch it in a try block.

PHP
<?php
try {
    throw new Exception("File could not be opened");
} catch (Exception $e) {
    echo $e->getMessage(), PHP_EOL;
    echo "Code: ", $e->getCode(), PHP_EOL;
}

How It Works

  1. throw new Exception(. ..) creates the object and jumps to the matching catch.
  2. getMessage() returns the text you passed to the constructor.
  3. No code was given, so getCode() returns the default value 0.
  4. PHP_EOL prints a line break after each value.

Example 2 — Message, Code, and Previous

Pass all three constructor arguments, including a previous exception for chaining.

PHP
<?php
$prev = new Exception("Disk I/O failed", 10);
$e = new Exception("Could not save report", 20, $prev);

echo $e->getMessage(), PHP_EOL;
echo $e->getCode(), PHP_EOL;
echo $e->getPrevious()->getMessage(), PHP_EOL;

How It Works

  1. The first Exception is the root cause with code 10.
  2. The second Exception wraps it and stores a higher-level message.
  3. getPrevious() returns the earlier Throwable.
  4. Chaining helps you keep both the user-facing error and the technical cause.

Example 3 — Custom Exception Constructor

Extend Exception, require a message, and forward everything to parent::__construct().

PHP
<?php
class AppException extends Exception
{
    public function __construct(string $message, int $code = 0, ?Throwable $previous = null)
    {
        parent::__construct($message, $code, $previous);
    }
}

try {
    throw new AppException("Invalid user id", 400);
} catch (AppException $e) {
    echo $e->getMessage(), " [", $e->getCode(), "]", PHP_EOL;
}

How It Works

  1. AppException extends Exception so it can be thrown and caught like any other exception.
  2. The custom constructor still calls parent::__construct() with message, code, and previous.
  3. throw new AppException(. ..) uses that constructor.
  4. getMessage() and getCode() work because the parent stored the values.

Frequently Asked Questions

It builds a new Exception with an optional message, code, and previous Throwable. You usually call it through new Exception(...) or a subclass constructor.
No. Writing new Exception("...") calls it for you. Call it explicitly only when a subclass constructor forwards arguments with parent::__construct().
It links a new exception to an older one. Later you can call getPrevious() to walk the chain and see the original cause.
Any integer that helps your app group errors, such as HTTP-like codes or internal enums. PHP does not assign special meaning to these numbers for Exception.
Because message, code, and previous are set in the parent. If you skip the call, getters like getMessage() will not show the values you intended.

Next: getMessage()

Exception::getMessage() returns the text message stored in the exception. That message is .

getMessage() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful