توابع set_exception_handler() در PHP

مثال

تعریف کاربر تعریف شده برای مدیریت استثنا:

<?php
// کاربر تعریف شده برای مدیریت استثنا
 function myException($exception) {
     echo '<b>Exception:</b> ', $exception->getMessage();
 }
 // Set user-defined exception handling function
 set_exception_handler("myException");
// Throw exception
throw new Exception("Uncaught exception occurred!");
 ?> 

The output of the above code is similar to this:

Exception: Uncaught exception occurred!

Definition and Usage

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

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

Syntax

set_exception_handler(exceptionhandler);
Parameters Description
exceptionhandler

Required. Specifies the name of the function to be called when an uncaught exception occurs.

Note:You can also pass a NULL value to reset the exception handler to the default value.

Technical Details

Return Value:

Returns a string containing the name of the previously defined exception handler, or NULL in case of an error.

If an error handler has not been defined previously, NULL is also returned.

If NULL is used for the parameter, the handler is reset to the default state and a TRUE is returned.

PHP Version: 5.0+
PHP Update Log:

PHP 7.0.0: Passed to exception_handler The parameter type has changed from Exception to Throwable.

Before PHP 5.5, if NULL is passed, the function returns TRUE. Starting from PHP 5.5, it returns the previous handler.