Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

set_exception_handler()

Example

Establish a user-defined function to handle exceptions.

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

Definition and Usage

The set_exception_handler() function establishes a user-defined function to handle exceptions.

 

The script will halt execution after the exception handler is invoked.

Syntax

set_exception_handler(exceptionhandler);

Parameter Values

Parameter

Description

exceptionhandler

Required. Specifies the name of the function to execute when an uncaught exception occurs. Passing NULL will reset the handler to its default state.

Technical Details

 

Return Value:

A string containing the previously defined exception handler, or NULL if no previous handler was set or if an error occurs.

PHP Version:

5.0+

PHP Changelog:

Previously, passing NULL to this function would return TRUE. Since PHP 5.5, it returns the previous handler instead.