PHP set_exception_handler()Functions


PHP Error 参考手册Complete PHP Error Reference Manual

Definition and Usage

The set_exception_handler() function sets a user-defined exception handling function.

This function is used to create the user's own exception handling method during runtime.

This function returns the old exception handler, or NULL on failure.

Syntax

set_exception_handler(exception_function)

Parameters Description
exception_function Required. Specifies the function to be called when an uncaught exception occurs.

This function must be defined before calling the set_exception_handler() function. This exception handling function needs one parameter, namely the thrown exception object.



Tips and Notes

Tip:After this exception handler is called, the script will stop executing.


Examples

<?php
function myException($exception)
{
echo "<b>Exception:</b> " , $exception->getMessage();
}

set_exception_handler('myException');

throw new Exception('Uncaught Exception occurred');
?>

The output of the above code is shown below:

Exception: Uncaught Exception occurred


PHP Error 参考手册Complete PHP Error Reference Manual Other Extensions