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);
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 |
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!");
?>