Computer >> Computer tutorials >  >> Programming >> PHP

set_exception_handler() function in PHP


The set_exception_handling() function sets a user-defined function to handle exceptions. It sets the default exception handler if an exception is not caught within a try/catch block. Execution will stop after the exception_handler is called.

Syntax

set_exception_handling(exception_handler)

Parameters

  • exception_handler − 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 that was thrown.

Return

The set_exception_hadler() function returns the name of the previously defined exception handler, or NULL on error. If no previous handler was defined, NULL is also returned.

Example

The following is an example −

<?php
   function exception_handler($exception) {
      echo "Uncaught exception = " , $exception->getMessage(), "\n";
   }
   set_exception_handler('exception_handler');
   throw new Exception('Not Found Exception');
   echo "not included Executed\n";
?>

Output

The following is the output −

Uncaught exception = Not Found Exception