PHP 7 Script to Handle Exceptions Using Try,Catch and Finally Block in Browser Full Project For Beginners

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments

 

Welcome folks today in this blog post we will be handling exceptions using try,catch and finally block in browser using php 7. All the full source code of the application is shown below.

 

 

 

Get Started

 

 

 

In order to get started you need to make an index.php file and copy paste the following code

 

 

index.php

 

 

 

<?php
function division($dividend, $divisor)
{
    if($divisor == 0)
        throw new Exception('Division by zero.');      // Throw exception if divisor is zero  
   else{
          $quotient = $dividend / $divisor;
          echo "<p>$dividend / $divisor = $quotient</p>";
           }
}
try
{
    division(15, 0);
}
 catch(Exception $e)       // Handle the exception
{
    echo "<p>Caught exception: " . $e->getMessage() . "</p>";
}
?>

Leave a Reply