English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP set_exception_handler() Function Usage and Example

PHP Error & Loggings Reference Manual

The set_exception_handler() function sets a user-defined exception handling function

Syntax

string set_exception_handler ( callback $exception_handler );

Definition and Usage

If try / If an exception is not caught in the catch block, this function sets the default exception handler. After calling exception_handler, execution will stop.

Parameter

Serial NumberParameters and Description
1

exception_handler

The name of the function to be called when an uncaught exception occurs. This function must be defined before calling set_exception_handler().

This handler function needs to accept one parameter, which will be the exception object thrown.

Return Value

It returns the name of the previously defined exception handler, or NULL if an error occurs. If no previous handler is defined, NULL is also returned.

Online Example

The following is the usage of this function-

<?php
   function exception_handler($exception) {
      echo "The uncaptured exception is: " , $exception->getMessage(), "\n";
   }
   
   set_exception_handler('exception_handler');
   set_exception_handler();
   
   throw new Exception('No exception found');
   echo "Not including the executed\n";
?>
Test and see‹/›

Output result:

The uncaptured exception is: No exception found

PHP Error & Loggings Reference Manual