Here we are going to see what is PHP Exceptions, How to throw exceptions, How to use the try..catch…finally statement, and how to create a PHP Exception object.
What is an Exception?
The exception is error handling methods that describe errors when an error or unexpected behavior accrued.
There are already many exceptions in the build-in PHP functions and classes.
We can also add exceptions in user-defined functions.
The exception itself is not an error but it indicates that there is an error.
Using Exceptions we can stop a function or provide different code blocks to run depending on our program.
Throwing Exception in PHP
Using throw
statement we can throw an exception in the user-defined function.
When we add this statement the code following this throw statement would not run.
Here is an easy example of how to throw an exception in a PHP function:
function divide($dividend, $divisor) {
if($divisor == 0) {
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
echo divide(5, 0);
Here we create a function to divide numbers.
The first number is divided and the second one is the divisor.
As we all know that we can divide a number with o.
and if we try to do this in PHP we would get a fatal error.
To avoid this using if condition we ensure that our program will throw an exception when the divisor is 0.
But throw statement alone note enough to avoid fatal errors we also have to catch statement.
The Try… catch statement
Using Throw we can throw exceptions on particular conditions but it is not always enough.
Depending on our program we also want to run alternative code instead when there is an exception present.
For this we use the try…catch statement.
The syntax for both those statements goes like this:
try {
//code to throw exceptions.
} catch (Exception $e){
// code that run when exception is cought.
}
Here is a real-life example of how we can use a try and catch statement to throw and catch exceptions:
function divide($dividend, $divisor) {
if($divisor == 0) {
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
try {
echo divide(5, 0);
} catch(Exception $e) {
echo "Can't divide a number by 0";
}
Here $e
inside catch represents what type of exception should be caught.
If you echo or print $e
you will get the exact error.
The try…catch…finally statement
The only difference between try...catch
and try...catch...finally
is the finally
.
If finally is present the second one catch
is optional.
Whatever we write in finally
will run regardless of if an exception is caught or not.
Here is the syntax for it:
try {
//code that can throw exceptions
} catch(Exception $e) {
//code that runs when an exception is caught
} finally {
//code that always runs regardless of whether an exception was caught
}
Here is a real-life example of how to use try…catch…finally-
function divide($dividend, $divisor) {
if($divisor == 0) {
throw new Exception("Division by zero");
}
return $dividend / $divisor;
}
try {
echo divide(5, 0);
} finally {
echo "Process complete.";
}
The Exception Object
If we want more control over exceptions we can use the Exception class to build a new exception object.
Here is the syntax of Exception object syntax-
new Exception(message, code, previous);
As you can see this object contains 3 parameters:
message
: This describe why is the exception was thrown. Its optional parameter.code
: This is integer that is for to distinguish this exception from others of the same type. This one is also optional.previous
: If we use this exception object to thrown in catch block on another exception. This one is also optional but recommended if that the case.
Apart from those parameters the Exception object also has a few methods that we can use to retrieve more information about the object:
getMessage()
: Return the message parameter.getPrevious()
: It returns the privious parameter of the exception object.getCode()
: Return the exception code.getFile()
: This function retrun path of the file where exception was thrown.getLine()
: Using this function we get the exact line number of code which threw the exception.
Here is how we can use exception objects with those methods:
function divide($dividend, $divisor) {
if($divisor == 0) {
throw new Exception("Division by zero", 1);
}
return $dividend / $divisor;
}
try {
echo divide(5, 0);
} catch(Exception $ex) {
$code = $ex->getCode();
$message = $ex->getMessage();
$file = $ex->getFile();
$line = $ex->getLine();
echo "Exception thrown in $file on line $line: [Code $code]
$message";