PHP get_exception_handler() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Error

Definition

In PHP, get_exception_handler() returns the function that currently handles uncaught exceptions, or null if none is set. It needs PHP 8.5 or newer.

You set an exception handler with set_exception_handler(). Until PHP 8.5 there was no direct way to look at it again. get_exception_handler() fixes that. Since it is new, wrap it in function_exists('get_exception_handler') if your code also needs to work on older PHP versions.

Syntax

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

PHP
$handler = get_exception_handler();
CallWhat it does
get_exception_handler()Returns the current exception handler, or null if there is none.
function_exists('get_exception_handler')Checks that your PHP version supports the function (PHP 8.5+).
set_exception_handler(get_exception_handler())Sets the same handler again, which is handy when restoring a saved one.

This function needs PHP 8.5 or newer. On older versions, calling it causes a fatal error, so guard it with function_exists(). It only reads the handler and never changes it.

Library declarations
PHP
get_exception_handler(): ?callable

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
?callableThe current exception handler exactly as it was given to set_exception_handler(), such as a function name, an array or a Closure. It returns null if no handler is set.

PHP Examples

Example 1 — Reading the Current Handler

At first there is no exception handler. After set_exception_handler(), get_exception_handler() returns its name. This example needs PHP 8.5 or newer.

PHP
<?php
if (!function_exists('get_exception_handler')) {
    echo "get_exception_handler() needs PHP 8.5 or newer.", PHP_EOL;
} else {
    function myExceptionHandler(Throwable $e): void {
        echo "Handled: ", $e->getMessage(), PHP_EOL;
    }

    var_dump(get_exception_handler());

    set_exception_handler('myExceptionHandler');
    echo get_exception_handler(), PHP_EOL;
}

How It Works

  1. function_exists() checks that this PHP version has get_exception_handler().
  2. No handler is set yet, so var_dump() prints NULL.
  3. set_exception_handler() registers myExceptionHandler by name.
  4. get_exception_handler() returns that same name, and we print it.

Example 2 — Comparing with a Closure

When the handler is an anonymous function, you get the very same Closure back.

PHP
<?php
if (!function_exists('get_exception_handler')) {
    echo "get_exception_handler() needs PHP 8.5 or newer.", PHP_EOL;
} else {
    $handler = function (Throwable $e): void {
        echo "Handled: ", $e->getMessage(), PHP_EOL;
    };

    set_exception_handler($handler);

    var_dump(get_exception_handler() === $handler);
    var_dump(get_exception_handler() instanceof Closure);
}

How It Works

  1. The anonymous function is stored in $handler and registered with set_exception_handler().
  2. get_exception_handler() returns the same object, so the strict comparison is true.
  3. A Closure is an object of the class Closure, so instanceof is true too.
  4. On older PHP versions the guard prints a message and the rest is skipped.

Example 3 — Saving and Restoring a Handler

Read the current handler, replace it for a while, and then put the original one back.

PHP
<?php
if (!function_exists('get_exception_handler')) {
    echo "get_exception_handler() needs PHP 8.5 or newer.", PHP_EOL;
} else {
    function firstHandler(Throwable $e): void {
        echo "First: ", $e->getMessage(), PHP_EOL;
    }

    function secondHandler(Throwable $e): void {
        echo "Second: ", $e->getMessage(), PHP_EOL;
    }

    set_exception_handler('firstHandler');
    $saved = get_exception_handler();

    set_exception_handler('secondHandler');
    echo "Temporary: ", get_exception_handler(), PHP_EOL;

    set_exception_handler($saved);
    echo "Restored: ", get_exception_handler(), PHP_EOL;
}

How It Works

  1. We register firstHandler and save the current handler in $saved using get_exception_handler().
  2. set_exception_handler('secondHandler') replaces it, and get_exception_handler() reports the new one.
  3. Passing $saved back to set_exception_handler() puts the original handler in place.
  4. get_exception_handler() confirms that firstHandler is active again.

Frequently Asked Questions

It returns the function currently set to handle uncaught exceptions, or null if there is none. It does not change anything.
PHP 8.5 and newer. Older versions do not have it, so check with function_exists('get_exception_handler') before you call it.
It returns null. In that case PHP shows its normal fatal error for an uncaught exception.
They called set_exception_handler() and used the return value, which is the previous handler. That changes the handler, which is why a simple getter is nicer.
No. It only deals with exception handlers. To read the error handler, use get_error_handler(), which is also new in PHP 8.5.

Next: restore_error_handler()

restore_error_handler() goes back to the error handler that was active before the most rec.

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