Error: An Error is an unexpected program result, which can not be handled by the program itself. That can be solved by using the issue in the code manually. An Error can be an infinite loop that can not be handled by the program itself so you have to manually repair that issue. There is an easy procedure to handle error i.e. using die() function.
Syntax:
die("message")
Program:
<?php // Open file in read mode $file_var = fopen("myfile.txt", "r"); // Check for file existence if (!file_exists("myfile.txt")) { die("Sorry Error!! file does not exists"); } else { fopen("myfile.txt", "r"); } ?> |
Output:
Sorry Error!! file does not exists
Exception: An Exception also is an unexpected result of a program but Exception can be handled by the program itself by throwing another exception. Exceptions should only be used with error conditions, where the error is non removal. There is an easy way to overcome the Exception by using try and catch method.
Syntax:
try {
}
catch {
}
Program:
<?php function valid_division($x, $y) { if($y != 0) { return true; } else { throw new Exception("Why should not be equal to 0"); } } try { valid_division(2, 0); // Try block will only run if // there is no exception echo "Valid division"; } catch(Exception $e) { // Catch block will run if an // exception occurs echo "Error\n"; echo $e->getmessage(); } ?> |
Output:
Error Why should not be equal to 0
Summary of Differences:
| ERROR | EXCEPTION |
|---|---|
| Error are the procedural approach. | Exceptions are an object-oriented approach to programming. |
| The default error handling in PHP is very simple. An error message with filename, line number and a message describing the error is sent to the browser. | Exceptions are used to change the normal flow of a script if a specified error occurs. |
| This can be done using PHP die() Function. | Basic Exception Handling using throw new Exception() in case of advance Exception handling youn have to use try and catch method. |
| Errors are mostly caused by the environment in which program is running. | Program itself is responsible for causing exceptions. |
Note: PHP is an exception light language by default, but you can change errors into exceptions when working with object-oriented code.
Recommended Posts:
- Generating Errors using HTTP-errors module in Node.js
- PHP | Predefined Exceptions
- How to log errors and warnings into a file in php?
- PHP | Types of Errors
- How do I get PHP errors to display?
- How to resolve unhandled exceptions in Node.js ?
- Scala | Finally Exceptions
- Scala | Try-Catch Exceptions
- Raising Exceptions in Ruby
- Which Keywords are used to handle Exceptions in JavaScript ?
- JavaScript | Errors - Throw and Try to Catch
- How to Create Custom Errors using New Function in Golang?
- Handling Errors in R Programming
- How to catch all JavaScript errors and send them to server?
- How to handle errors in node.js ?
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- Maximum execution time taken by a PHP Script
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai

