Explain PHP Exceptions
PHP Exceptions are a way to handle errors gracefully in your code. An exception is an object that describes an error or unexpected behavior in a PHP script. When an error occurs, an exception can be thrown, and it can be caught using a try-catch block. This allows you to handle errors in a structured way, making your code more robust and easier to debug.
Here's a basic example to illustrate how exceptions work in PHP:
Basic Example
<?php
function divide($dividend, $divisor) {
if($divisor == 0) {
throw new Exception("Division by zero.");
}
return $dividend / $divisor;
}
try {
echo divide(10, 2); // Outputs: 5
echo divide(10, 0); // Throws an exception
} catch (Exception $e) {
echo "Caught exception: " . $e->getMessage();
}
?>
Output
5
Caught exception: Division by zero.
Custom Exception Class
You can also create custom exception classes to handle specific types of exceptions.
<?php
class DivideByZeroException extends Exception {}
function divide($dividend, $divisor) {
if($divisor == 0) {
throw new DivideByZeroException("Cannot divide by zero.");
}
return $dividend / $divisor;
}
try {
echo divide(10, 2); // Outputs: 5
echo divide(10, 0); // Throws an exception
} catch (DivideByZeroException $e) {
echo "Caught DivideByZeroException: " . $e->getMessage();
} catch (Exception $e) {
echo "Caught general exception: " . $e->getMessage();
}
?>
Output
5
Caught DivideByZeroException: Cannot divide by zero.
Multiple Catch Blocks
You can catch different types of exceptions using multiple catch blocks.
<?php
class DivideByZeroException extends Exception {}
class NegativeNumberException extends Exception {}
function divide($dividend, $divisor) {
if($divisor == 0) {
throw new DivideByZeroException("Cannot divide by zero.");
}
if($dividend < 0 || $divisor < 0) {
throw new NegativeNumberException("Negative numbers are not allowed.");
}
return $dividend / $divisor;
}
try {
echo divide(10, 2); // Outputs: 5
echo divide(10, 0); // Throws DivideByZeroException
echo divide(-10, 2); // Throws NegativeNumberException
} catch (DivideByZeroException $e) {
echo "Caught DivideByZeroException: " . $e->getMessage();
} catch (NegativeNumberException $e) {
echo "Caught NegativeNumberException: " . $e->getMessage();
} catch (Exception $e) {
echo "Caught general exception: " . $e->getMessage();
}
?>
Output
5
Caught DivideByZeroException: Cannot divide by zero.
Finally Block
You can use a finally block to execute code regardless of whether an exception was thrown or not.
<?php
function divide($dividend, $divisor) {
if($divisor == 0) {
throw new Exception("Division by zero.");
}
return $dividend / $divisor;
}
try {
echo divide(10, 2); // Outputs: 5
echo divide(10, 0); // Throws an exception
} catch (Exception $e) {
echo "Caught exception: " . $e->getMessage();
} finally {
echo "This is the finally block.";
}
?>
Output
5
Caught exception: Division by zero.
This is the finally block.
Summary
What are PHP Exceptions?
Exceptions are objects used to handle runtime errors in a structured manner instead of allowing the script to terminate unexpectedly.
Try and Catch
The try block contains code that may throw an exception, while the catch block handles the exception if one occurs.
Custom Exceptions
You can create your own exception classes by extending the Exception class to handle specific error conditions.
Multiple Catch Blocks
PHP allows multiple catch blocks so different exception types can be handled separately.
Finally Block
The finally block always executes, whether an exception is thrown or not, making it useful for cleanup tasks.
Exceptions in PHP allow you to handle errors more effectively and ensure that your code can handle unexpected situations without crashing.
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
