PHP program to handle modulo by zero exception

PHP program to handle modulo by zero exception

Here, we will figure out how to deal with an exemption modulo by zero which happens when we partition (utilizing the modulus administrator to discover leftover portion) a number by 0?

The modulo by zero mistakes” tosses when we partition a number by zero to get the rest of modulus administrator (%).

It tends to be dealt with by utilizing “try…catch” articulation, with DivisionByZeroError special case.

Example:

<?php
    $a = 10;
    $b = 3;
    
    try 
    {
        //dividing $a by $b - no error 
        $result = $a%$b;
        print("result: $result \n");
        
        //assigning 0 to $b
        $b = 0;

        //now, dividing $a by $b - error occurs
        $result = $a%$b;
        print("result: $result \n");        
    }
    catch(DivisionByZeroError $err){
        print("Exception... ");
        print($err->getMessage());
    }
?>

Output

result: 1
Exception... Modulo by zero

Recent Posts

error: Alert: Content is protected!!