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);
Call
What 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
Parameter
Description
message
Optional. The text that describes what went wrong. The default is an empty string.
code
Optional. An integer you choose for your own error categories. The default is 0.
previous
Optional. Another Throwable that caused this one. Use it for exception chaining. The default is null.
Return Value
Return Value
Description
Exception
A 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;
}
Output
File could not be opened
Code: 0
How It Works
throw new Exception(. ..) creates the object and jumps to the matching catch.
getMessage() returns the text you passed to the constructor.
No code was given, so getCode() returns the default value 0.
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;
Output
Could not save report
20
Disk I/O failed
How It Works
The first Exception is the root cause with code 10.
The second Exception wraps it and stores a higher-level message.
getPrevious() returns the earlier Throwable.
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;
}
Output
Invalid user id [400]
How It Works
AppException extends Exception so it can be thrown and caught like any other exception.
The custom constructor still calls parent::__construct() with message, code, and previous.
throw new AppException(. ..) uses that constructor.
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.