PHP getCode() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Exception

Definition

In PHP, Exception::getCode() returns the integer code stored in the exception. You set that code when you create the Exception, or leave the default value 0.

Codes are useful when your app groups errors, for example 404 for missing data or 500 for unexpected failures. The method is final. Note that some SPL exceptions historically returned other types, but Exception itself returns an int.

Syntax

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

PHP
$code = $e->getCode();
CallWhat it does
$e->getCode()Returns the integer exception code.
(new Exception("x", 42))->getCode()Reads the code 42 from a new Exception.
match ($e->getCode()) { 404 => "missing", default => "other" }Branches on the code with match.

getCode() is final. For Exception the return type is int. Pass the code as the second constructor argument.

Library declarations
PHP
final public Exception::getCode(): int

Parameters

ParameterDescription
—This method takes no parameters.

Return Value

Return ValueDescription
intThe exception code. The default is 0 when you omit it in the constructor.

PHP Examples

Example 1 — Read a Custom Error Code

Throw an Exception with code 404 and print it from the catch block.

PHP
<?php
try {
    throw new Exception("User not found", 404);
} catch (Exception $e) {
    echo $e->getCode(), PHP_EOL;
    echo $e->getMessage(), PHP_EOL;
}

How It Works

  1. The second constructor argument sets the code to 404.
  2. catch receives the Exception object.
  3. getCode() returns 404.
  4. getMessage() still returns the text message.

Example 2 — Default Code Is Zero

Create an Exception without a code and confirm getCode() returns 0.

PHP
<?php
$e = new Exception("Something failed");
var_dump($e->getCode());

How It Works

  1. Only the message was passed.
  2. The default code is 0.
  3. getCode() returns integer 0.
  4. Treat 0 as “no special code” unless your project assigns another meaning.

Example 3 — Branch on the Code

Use getCode() to choose a short status label for an API-style response.

PHP
<?php
function statusLabel(Exception $e): string
{
    return match ($e->getCode()) {
        400 => "bad_request",
        404 => "not_found",
        500 => "server_error",
        default => "error",
    };
}

echo statusLabel(new Exception("Missing id", 404)), PHP_EOL;
echo statusLabel(new Exception("Boom")), PHP_EOL;

How It Works

  1. match compares the integer from getCode().
  2. 404 maps to not_found.
  3. An Exception with the default code 0 falls into default.
  4. This pattern keeps HTTP-like labels out of the message text.

Frequently Asked Questions

For Exception it returns an integer code set in the constructor. The default is 0.
Your application does. PHP stores the number but does not map Exception codes to HTTP status codes by itself.
No. It is final. Put custom logic in your own methods instead.
No. You can pass only a message. In that case getCode() returns 0.
getMessage() is human-readable text. getCode() is a compact number for programs to branch on or filter in logs.

Next: getFile()

Exception::getFile() returns the path of the PHP file where the exception object was creat.

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