Header Ads Widget

Responsive Advertisement

Ticker

6/recent/ticker-posts

PHP Exception & Error Handling

 What is an Exception?

An error is an unexpected program result that cannot be handled by the program itself. Errors are resolved by fixing the program. An example of an error would be an infinite loop that never stops executing. An exception is an unexpected program result that can be handled by the program itself.

An exception is a signal that indicates some sort of exceptional event or error has occurred. Exceptions can be caused due to various reasons, for example, database connection or query fails, file that you’re trying to access doesn’t exist, and so on.

Why handle the exception?

  • Avoid unexpected results on our pages which can be very annoying or irritating to our end users.
  • Improve the security of our applications by not exposing information that malicious users may use to attack our applications.
  • PHP Exceptions are used to change the normal flow of a program if any predictable error occurs.

PHP Error handling
When an error occurs, depending on your configuration settings, PHP displays the error message in the web browser with information relating to the error that occurred.

PHP offers a number of ways to handle errors. We are going to look at three (3) commonly used methods.

  1. Die statements – The die function combines the echo and exit function in one. It is very useful when we want to output a message and stop the script execution when an error occurs.
  2. Custom error handlers – These are user-defined functions that are called whenever an error occurs.
  3. PHP error reporting – the error message depending on your PHP error reporting settings. This method is very useful in a development environment when you have no idea what caused the error. The information displayed can help you debug your application.

Using Throw and Try…Catch Statements
In an exception-based approach, program code is written in a try block, an exception can be thrown using the throw statement when an exceptional event occurs during the execution of code in a try block. It is then caught and resolved by one or more catch blocks.

The following example demonstrates how exception handling works:

<?php
function division($dividend, $divisor)
{
// Throw exception if divisor is zero
if($divisor == 0){
throw new Exception(‘Division by zero.’);
}
else
{
$quotient = $dividend / $divisor;
echo “<p>$dividend / $divisor = $quotient</p>”;
}
}

try
{
division(10, 2);
division(30, -4);
division(15, 0);

// If exception is thrown following line won’t execute
echo ‘<p>All divisions performed successfully.</p>’;
}
catch(Exception $e)
{
// Handle the exception
echo “<p>Caught exception: ” . $e->getMessage() . “</p>”;
}

// Continue execution
echo “<p>Hello World!</p>”;
?>

 
If you have any queries or suggestions, feel free to ask me via the comment section below.

must watch some other topics :

cake PHP training institute
cake PHP training course
cake php training online

Post a Comment

0 Comments