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.
Exceptions in PHP allow you to handle errors more effectively and ensure that your code can handle unexpected situations without crashing.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.