PHP set_exception_handler() Function

PHP trigger_error() Function
PHP set_error_handler() Function

In this article, you will learn how to create exception handler with a user-defined function in PHP.

The set_exception_handler() function in PHP sets a user-defined exception handler function. The script will stop executing after the exception handler is called.

what is the syntax of the SET_EXCEPTION_HANDLER() function in php?

set_exception_handler(exceptionhandler);
ParameterDescription
exceptionhandlerRequired. 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
PHP SET_EXCEPTION_HANDLER() method

examples of the SET_EXCEPTION_HANDLER() function

Example 1. In this example, we 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!");
?>
PHP trigger_error() Function
PHP set_error_handler() Function
en English
X
Scroll to Top