YouTip LogoYouTip

Func Error Set Exception Handler

```html PHP set_exception_handler() Function body { font-family: Arial, sans-serif; margin: 0; padding: 20px; background-color: #f5f5f5; } .container { max-width: 1200px; margin: 0 auto; background: white; padding: 20px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); } h1 { color: #333; } .breadcrumb { margin-bottom: 20px; color: #666; } .breadcrumb a { color: #337ab7; text-decoration: none; } .code-block { background: #f4f4f4; padding: 15px; border-radius: 5px; margin: 15px 0; overflow-x: auto; } .example { background: #fff3cd; padding: 15px; border-left: 4px solid #ffc107; margin: 15px 0; } .note { background: #d1ecf1; padding: 15px; border-left: 4px solid #17a2b8; margin: 15px 0; } .warning { background: #f8d7da; padding: 15px; border-left: 4px solid #dc3545; margin: 15px 0; } table { width: 100%; border-collapse: collapse; margin: 15px 0; } th, td { border: 1px solid #ddd; padding: 12px; text-align: left; } th { background-color: #f2f2f2; } .sidebar { float: right; width: 280px; margin-left: 20px; } .content { margin-right: 300px; }

PHP set_exception_handler() Function

PHP Runtime Error Handling

Definition and Usage

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

This function returns the name of the previously defined exception handler, or NULL on error. If the function requires parameters, the name should be passed as a string, otherwise the function name can be passed directly as a string or as an array (see the examples below).

Syntax

set_exception_handler(callback $exception_handler): mixed

Parameters

Parameter Description
exception_handler Required. The name of the function to be called when an uncaught exception occurs. This function must be defined before calling set_exception_handler(). The handler function must accept one parameter, which will be the exception object thrown.

Technical Details

Return Value: Returns the name of the previously defined exception handler, or NULL on error.
PHP Version: PHP 5+
Changelog: PHP 7: The return type was changed to ?string. Previously returned string or NULL (depending on whether a previous handler was set).

More Examples

Example 1

How to use set_exception_handler():

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

set_exception_handler("myException");

throw new Exception("Uncaught Exception!");
?>

Output of the above code:

Exception: Uncaught Exception!

Example 2

Using set_exception_handler() with a class:

<?php
class MyExceptionHandler {
    public function handle($exception) {
        echo "Exception: " . $exception->getMessage();
    }
}

set_exception_handler(array("MyExceptionHandler", "handle"));

throw new Exception("This exception will be caught by the class handler!");
?>

Related Functions

```
← Func Error Trigger ErrorFunc Error Set Error Handler β†’