In this article Exceptions in PHP we give the information about Error Handling and Exceptions are used in PHP to make the code safe from errors.
This ensures that even when an error occurs, your application does not crash and an appropriate message or solution can be provided to the user.
Exceptions in PHP
Error Handling in PHP:
Error handling means detecting errors in a program, handling them, and notifying the user. There are mainly three types of errors in PHP:
- Notice: Minor errors that do not affect the application.
- Warning: Warning errors that do not crash the application.
- Fatal Error: Serious errors that cause the code to stop.
Ways to Handle Errors in PHP
- Use of die() function
If an error occurs, die() is used to stop the application and display a message.
<?php
$file = “test.txt”;
// try to open the file
if (!file_exists($file)) {
die(“Error: File not found.”);
}
$handle = fopen($file, “r”);
?>
- Creating Custom Error Handler
In PHP you can create your custom error handler.
Example: Custom Error Handler
<?php
function customError($errno, $errstr) {
echo “<b>Error:</b> [$errno] $errstr<br>”;
}
// set custom error handler
set_error_handler(“customError”);
// generate error
echo 10/0; //Division by zero
?>
Exceptions in PHP:
Exceptions are special types of errors that disrupt the normal flow of the program. When an exception is thrown, it needs to be caught and handled.
How to use Exceptions?
- try-catch block
try-catch is used to throw and catch Exception.
Example: use of try-catch
<?php
function divide($num1, $num2) {
if ($num2 == 0) {
throw new Exception(“Divide by zero is not possible.”);
}
return $num1 / $num2;
}
try {
echo divide(10, 2); // Correct
echo divide(10, 0); // will throw exception
} catch (Exception $e) {
echo “Error: ” .$e->getMessage();
}
?>
Output:
Error: Divide by zero is not possible.
- finally block
finally is used to run the code in any condition (regardless of whether an Exception occurs or not).
Example: try-catch-finally
<?php
function openFile($filename) {
if (!file_exists($filename)) {
throw new Exception(“File not found.”);
}
return fopen($filename, “r”);
}
try {
openFile(“test.txt”);
} catch (Exception $e) {
echo “Error: ” .$e->getMessage();
} finally {
echo “<br>This code will run forever.”;
}
?>
Output:
Error: File not found.
This code will run forever.
-
Handling Multiple Exceptions
Example: Different handling for different Exceptions
<?php
class DivideByZeroException extends Exception {}
class NegativeNumberException extends Exception {}
function calculate($num1, $num2) {
if ($num2 == 0) {
throw new DivideByZeroException(“Divide by zero is not possible.”);
}
if ($num1 < 0 || $num2 < 0) {
throw new NegativeNumberException(“Negative numbers are not allowed.”);
}
return $num1 / $num2;
}
try {
echo calculate(-10, 5);
} catch (DivideByZeroException $e) {
echo “Error: ” .$e->getMessage();
} catch (NegativeNumberException $e) {
echo “Error: ” .$e->getMessage();
}
?>
Output:
Error: Negative numbers are not allowed.
Using Errors and Exceptions together in PHP
In PHP you can handle both errors and exceptions together.
Example: Custom Error and Exception Handler
<?php
function customError($errno, $errstr) {
echo “<b>Error:</b> [$errno] $errstr<br>”;
}
set_error_handler(“customError”);
function divide($num1, $num2) {
if ($num2 == 0) {
throw new Exception(“Divide by zero is not possible.”);
}
return $num1 / $num2;
}
try {
echo divide(10, 0);
} catch (Exception $e) {
echo “Exception: ” $e->getMessage();
}
?>
Conclusion
- Error Handling is used to handle common errors.
- Exceptions are used to handle special situations in the code.
- Make code safe and stable by using try-catch-finally.
- Use custom handlers to handle errors in complex applications.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining