HomeMenu
Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

set_exception_handler

Description

The set_exception_handler of Error Handling for PHP sets a user-defined exception handler function.

Syntax

set_exception_handler(
    ?callable $callback
): ?callable

Parameters

callback

The function to be called when an uncaught exception occurs. This handler function needs to accept one parameter, which will be the Throwable object that was thrown. Both Error and Exception implement the Throwable interface.

handler(
    Throwable $ex
): void

null may be passed instead, to reset this handler to its default state.

Return

Returns the previously defined exception handler, or null on error. If no previous handler was defined, null is also returned.

Examples

1 · callback

<?

function handler($ex)
{
    echo __FUNCTION__. PHP_EOL.
    $ex->getMessage();
}

$callback = "handler";

set_exception_handler($callback);

throw new Exception("exception");
handler
exception

2 · return

<?

function handler($ex)
{
    echo __FUNCTION__. PHP_EOL.
    $ex->getMessage();
}

$callback = "handler";

$return = set_exception_handler($callback);

var_dump($return);

$return = set_exception_handler($callback);

var_dump($return);
NULL
string(7) "handler"