Latest web development tutorials
 

PHP set_exception_handler() Function

< PHP Error Reference

Example

Set a user-defined exception handler function:

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

// Set user-defined exception handler function
set_exception_handler("myException");

// Throw exception
throw new Exception("Uncaught exception occurred!");
?>

The above code will output something like this:

Exception: Uncaught exception occurred!


Definition and Usage

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

The script will stop executing after the exception handler is called. 


Syntax

set_exception_handler(exceptionhandler);

Parameter Description
exceptionhandler Required. Specifies the name of the function to be run when an uncaught exception occurs. NULL can be passed instead, to reset this handler to its default state

Technical Details

Return Value: A string that contains the previously defined exception handler, or NULL on error or if no previous handler was defined
PHP Version: 5.0+
PHP Changelog: Previously, if NULL was passed then this function returned TRUE. It returns the previous handler since PHP 5.5

< PHP Error Reference