Exceptions are used to handle the error which is occurs when run a PHP script. In this post, we will learn about PHP Exception Handling and learn to use Try Catch block.


PHP Exception Handling - Try, Throw and Catch


PHP Exception Handling

To better understand the PHP Exception Handling, let us look at the given example. In this example we divide 2 by 0 (zero). But we know that we can not divide by zero. For this reason this code not run and give an error. 

<?php
$num = 0;
echo 2/$num;
?> code-box

Output : 
Warning: Division by zero in this file.
INF

We can easily handle this error by using if else. In this given example, we use if else and after this we do not get an error in our code.

<?php
$num = 0;
if($num>0) {
echo 2/$num;
}else {
echo "Please provide valid number";
}
?> code-box

Output : 
Please provide valid number

We get this above output. Now you might be thinking that we have successfully handled the error. But you are wrong. This is not the proper way to handle the error in PHP. To handle the error in PHP we will use Try Catch block. About Try Catch we will know further.

Try, Throw and Catch

In PHP we use try, throw and catch instead of if-else to handle the error in PHP. So what is try, throw and catch. We will know the answer of this question in next few lines.

try : We keep our code in this function. If this code does not give any error then this code run normally and when this code gives any error then this function throw an exception.

throw : This throw is handle by the catch function. Each throw must have at least one catch. 

catch : This function catch the throw and give a message as output according to errors.

Now we will learn how to use try, throw and catch with the help of  this given example.

<?php
$num = 0;

try {
if($num==0) {
throw new Exception('Please provide valid number');
}
echo 2/$num;
} catch(Exception $e) {
echo $e->getMessage();

echo $e->getLine();
echo $e->getCode();
echo $e->getFile();}

?> code-box

Output : 
Please provide valid number

$e->getMessage() - Print the exception message.
$e->getLine() - Give the line number.
$e->getCode() - For get the code.
$e->getFile() - For get file name.


We get an output. This output is similar to the output of the previous example. But this example code is different form the code of the previous example. In this example we use try catch block. In try block we take an if condition. If this condition returns an error then this error throw by this try block. Then catch block get it and give information about this exception.
 

Custom Exception Class

If we want to create a custom exception class then we will use custom function for this. The custom exception class inherits the properties form the exception class. In this given below code we have created a custom exception class.

<?php
$num = 4;

class customException extends Exception {
function errorMessage() {
return $this->getMessage();
}
}

try {
if($num==0) {
throw new Exception('Please Valid Number');
}
if($num==4) {
throw new customException('This is custom exception class');
}
echo 2/$num;
} catch(Exception $e) {
echo $e->getMessage();
} catch(customException $e) {
echo $e->getMessage();
} finally {
echo "Finally";

}
?> code-box

Output : 
This is custom exception class

In this above example we create a customException class and inherits the property of exception class. Finally block use to print the "finally" end of the code. We use multiple exceptions classes which is return different error messages.

Summary

In this post we learn about PHP exception handling and learn to use Try Catch block. If you have any doubt, then comment it. Keep visiting the blog to read more similar posts.

Post a Comment

Previous Post Next Post