
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Errors in PHP7
Introduction
Prior to version 7, PHP parser used to report errors in response to various conditions. Each error used to be of a certain predefined type. PHP7 has changed the mechanism of error reporting. Instead of traditional error reporting, most errors are now reported by throwing error exceptions.
If error exceptions go unhandled, a fatal error is reported and will be handled like traditional error condition. PHP's error heirarchy starts from Throwable interface. All predefined errors such as ArithmeticError, AssertionError, CompileError and TypeError are classes implementing Throwable iterface. Exception in PHP 7 is also implements Throwable interface.
Throwable interface acts as base for any object that can be thrown via throw statement, including Error and Exception objects. A user defined class can not implement Throwable interface directly. Instead, to declare a user defined exception class, it must extend Exception class.
PHP's exception handling consists of throw, catch, try statements. To raise exception object, there is throw keyword. Thrown exception are processed by catch block. PHP code that is potentially vulnerable for exception is enclosed in try block.
One or more catch blocks may be present to catch different instances of Exception classes. When no exception occurs in try block, normal execution will continue after last catch block. However, when exception is thrown inside try block, instead of nest statement being executed, PHP tries to find a catch block tht matches type of exception to be handled. If no matching catch block is defined, PHP parser will report Fatal error with Uncaught Exception message.
You may also provide finally block either after or instead of catch blocks. Code in finally block will always be executed, irresepective of whether exception occurs inside try block or not.
- Related Articles
- PHP Types of Errors
- How to display errors in PHP file?
- Errors in Assessment
- Difference between Compile Time Errors and Runtime Errors in C Program
- Errors in C/C++
- Custom Errors in JavaScript
- Handling Errors in TypeScript
- Database Handling Errors in Python
- What is the difference between compile time errors and run time errors in Java?
- What are syntax errors in JavaScript?
- What are runtime errors in JavaScript?
- Examples of runtime errors in Python?
- Errors within Perl Modules
- How to catch syntax errors in JavaScript?
- Handling errors in SAP GUI Scripting code
