PHP

How to create Divide By Zero Exception class in PHP | PHP program

How to create Divide By Zero Exception class in PHP | PHP program

How to create Divide By Zero Exception class in PHP | PHP program

We will create a user-defined class DivideByZeroException that inherits the Exception class to handle the divide by zero situation using the PHP program.

<?php



class DivideByZeroException extends Exception
{
}

try
{
    $A = 10;
    $B = 0;

    if ($B == 0) throw new DivideByZeroException("Divide by Zero exception");
    $C = $A / $B;
    printf("Value of C: %d<br>", $C);
}
catch(DivideByZeroException $e)
{
    printf("Exception: %s", $e->getMessage());
}
?>
TutorialsLink Curator

This is an official account managed by the team TutorialsLink Community