
- 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
Differentiate between exception and error in PHP
Let's discuss the differences between errors and exceptions.
- Recovering from Error is not possible. The only solution to errors is to terminate the execution. Whereas we can recover from Exception by using either try-catch blocks or throwing an exception back to the caller.
- You will not be able to handle the Errors using try-catch blocks. Even if you handle them using try-catch blocks, your application will not recover if they happen. On the other hand, Exceptions can be handled using try-catch blocks and can make program flow normally if they happen.
- Exceptions are related to the application whereas Errors are related to the environment in which application is running.
Example
<?php try { $row->insert(); $inserted = true; } catch (Exception $e) { echo "There was an error inserting the row - ".$e->getMessage(); $inserted = false; } echo "Some more stuff"; ?>
Explanation
Program execution will continue - because you 'caught' the exception. An exception will be treated as an error unless it is caught. It will allow you to continue program execution after it fails as well.
Example
<?php $foo = [bar]; echo $foo; ?>
Explanation
Program execution will stop with PHP Notice: Array to string conversion.
- Related Articles
- Difference between Exception and Error in Java
- Difference Between Error and Exception in Java
- What are the differences between an Exception class and an Error class in Java?\n
- Exceptions and Error in PHP 7
- PHP Nested Exception
- Difference between system level exception and Application level exception.
- What is Exception Handling in PHP ?
- Difference between Interrupt and Exception
- How to print the Python Exception/Error Hierarchy?
- Differentiate between turnover and profit.
- Differentiate between ADR AND GDR.
- Differentiate between invoice and bill.
- Differentiate between investing and trading.
- Differentiate between company and firm.
- Differentiate between finance and accounting.

Advertisements