
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
PHP Error Handling set_exception_handler() Function
The PHP Error Handling set_exception_handler() function is used to handle exceptions. If an uncaught exception occurs, you can use it to define a function that will be called. This helps you manage errors more effectively by giving you control over how exceptions are recorded or displayed. Instead of using the normal error message, you can create a more friendly response.
You can use this function to generate error logs or set off alarms when something goes wrong with your code. The set_exception_handler() function optimizes your website's error handling. It improves your application's security and stability.
Syntax
Below is the syntax of the PHP Error Handling set_exception_handler() function −
Callable set_exception_handler ( ?callable $callback )
Parameters
This function accepts $callback parameter which is the function that will be run in the case of an uncaught exception. This handler function just has to accept the Throwable object that was thrown as an argument. Error and Exception both implement the Throwable interface.
Here is the signature for the $callback function −
void handler ( Throwable $ex )
Here $ex is the variable that represents the exception or error that was thrown.
Return Value
The set_exception_handler() function returns null if the previously selected exception handler or an error occurs. If no previous handler was defined, null is also returned.
PHP Version
First introduced in core PHP 5, the set_exception_handler() function continues to function easily in PHP 7, and PHP 8.
Example 1
This simple program has a basic exception handler setup to identify exceptions and display the error message with the help of the PHP Error Handling set_exception_handler() function. When an exception occurs, the handler is called to show the message.
<?php // Set an exception handler set_exception_handler(function($ex) { echo "Exception caught: " . $ex->getMessage(); }); // Throw an exception throw new Exception("Something went wrong!"); ?>
Output
Here is the outcome of the following code −
Exception caught: Something went wrong!
Example 2
In the below PHP code we will use the set_exception_handler() function and set an exception handler that logs the exception message to a file. It helps to keep track of errors without showing them to users.
<?php // Set an exception handler to log errors set_exception_handler(function($ex) { // Log the exception message to a file file_put_contents('error_log.txt', $ex->getMessage() . "\n", FILE_APPEND); echo "Error has been logged."; }); // Throw an exception throw new Exception("Database connection failed!"); ?>
Output
This will generate the below output −
Error has been logged.
Example 3
This program sends an email to the admin whenever an exception occurs with the help of the set_exception_handler() function. It helps in critical situations to notify the concerned person about the error immediately.
<?php // Set an exception handler to send an email alert set_exception_handler(function($ex) { // Send an email with the exception details mail("admin@example.com", "Error Alert", $ex->getMessage()); echo "Error alert has been sent to the admin."; }); // Throw an exception throw new Exception("Critical system failure!"); ?>
Output
This will create the below output −
Error alert has been sent to the admin.