PHP getMessage() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Exception

Definition

In PHP, Exception::getMessage() returns the text message stored in the exception. That message is usually the string you passed when you created the Exception.

Call getMessage() inside a catch block to show a clear error to the user or to write it to a log. The method is final, so subclasses cannot override it. Use __toString() or your own methods if you need a custom format.

Syntax

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

PHP
$text = $e->getMessage();
CallWhat it does
$e->getMessage()Returns the exception message string.
(new Exception("Oops"))->getMessage()Creates a temporary Exception and reads its message in one expression.
$e->getPrevious()->getMessage()Reads the message of the previous exception in a chain.

getMessage() is final and always returns a string. An empty constructor message gives an empty string, not null.

Library declarations
PHP
final public Exception::getMessage(): string

Parameters

ParameterDescription
—This method takes no parameters.

Return Value

Return ValueDescription
stringThe exception message. It may be an empty string if none was set.

PHP Examples

Example 1 — Read the Message in a Catch Block

Throw an Exception with a clear message and print it after catching.

PHP
<?php
try {
    throw new Exception("Username is required");
} catch (Exception $e) {
    echo "Error: ", $e->getMessage(), PHP_EOL;
}

How It Works

  1. throw creates an Exception with the given message.
  2. catch Exception $e receives that object.
  3. getMessage() returns Username is required.
  4. We prefix the text with Error: for a simple display.

Example 2 — Empty Message

See what getMessage() returns when you create an Exception with no text.

PHP
<?php
$e = new Exception();
var_dump($e->getMessage());

$e2 = new Exception("");
var_dump($e2->getMessage());

How It Works

  1. new Exception() uses the default empty message.
  2. getMessage() returns an empty string, not null.
  3. Passing "" explicitly does the same thing.
  4. Check the string length or emptiness if your UI needs a fallback message.

Example 3 — Log Message and Code Together

Combine getMessage() with getCode() for a short log line.

PHP
<?php
try {
    throw new Exception("Payment gateway timeout", 504);
} catch (Exception $e) {
    echo "[", $e->getCode(), "] ", $e->getMessage(), PHP_EOL;
}

How It Works

  1. The constructor stores both a message and a code.
  2. getCode() returns 504.
  3. getMessage() returns the readable text.
  4. Putting them on one line makes log searching easier.

Frequently Asked Questions

It returns the string message stored in the Exception. That is usually the first argument you passed to new Exception(...).
No. It is final. Override __toString() or add your own method if you need different wording.
No. It always returns a string. If no message was set, you get an empty string.
Most often inside catch, when you write a log entry, build an API error body, or show a friendly message to the user.
getMessage() returns only the message text. __toString() builds a longer string that includes class name, code, file, line, and stack trace.

Next: getPrevious()

Exception::getPrevious() returns the previous Throwable that was passed when the exception.

getPrevious() 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