PHP find output programs (Exceptions) | set 2

Find the output of PHP programs | Exceptions | Set 2: Enhance the knowledge of PHP Exceptions concepts by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 26, 2021

Question 1:

<?php
    $C = 0;

    function divide($A, $B)
    {
        try
        {
            if ($B == 0) throw new Exception("Divide By Zero");
            $GLOBALS['C'] = $A / $B;
        }
    }
    
    divide(10, 5);

    catch(Exception $E)
    {
        echo "<br>" . $E->getMessage();
    }
    
    echo '<br>' . $C;
?>

Output:

PHP Parse error:  syntax error, unexpected 'catch' (T_CATCH), 
expecting end of file in /home/main.php on line 15

Explanation:

The above program will generate compilation error because try and catch must be with the same scope.

Here, we declared try block inside the divide() function and catch function is defined outside the divide() function.

Question 2:

<?php
    $C = 0;
    
    function divide($A, $B)
    {
        if ($B == 0) throw new Exception("Divide By Zero");
        $GLOBALS['C'] = $A / $B;
    
    }
    
    try
    {
        divide(10, 0);
    }
    catch(Exception $E)
    {
        echo "<br>" . $E->getMessage();
    }
    
    echo '<br>' . $C;
?>

Output:

Divide By Zero
0

Explanation:

In the above program, we defined a function divide(), here if the value of argument $B is 0. Then it will trough exception, otherwise, it will calculate expression and assign the result into a global variable $C.

We called divide() function inside the try block with 10,0 argument, here 10 will assign to argument $A and 0 will assign in argument $B then it will throw an exception, which is caught by catch block that will print "Divide By Zero" and then print the value of $C that is 0 on the webpage.

Question 3:

<?php
    $C = 0;
    
    class MyException extends Exception
    {
        public function expMesg()
        {
            echo "Custom:" . $this->getMessage();
        }
    }

    function divide($A, $B)
    {
        if ($B == 0) throw new MyException("Divide By Zero");
        $GLOBALS['C'] = $A / $B;
    
    }
    
    try
    {
        divide(10, 0);
    }
    catch(MyException $E)
    {
        echo "<br>" . $E->expMesg();
    }
    
    echo '<br>' . $C;
?>

Output:

Custom:Divide By Zero

0

Explanation:

In the above program, we created class extended by Exception class and defined a member function expMesg() that will print the exception message.

Then we defined a function divide(), here if the value of argument $B is 0. Then it will trough the object of MyException class, otherwise, it will calculate expression and assign the result into a global variable $C.

We called divide() function inside the try block with 10,0 argument, here 10 will assign to argument $A and 0 will assign in argument $B then it will throw an object of MyException(), which is caught by catch block that will print "Divide By Zero" and then print the value of $C that is 0 on the webpage.

Question 4:

<?php
    $A = 20;
    $B = 0;
    $C = 0;
    
    try
    {
        if ($B == 0) throw new Exception("Divide By Zero");
        $C = $A / $B;
    }
    catch(Exception $E)
    {
        throw $E;
    }
    catch(Exception $E)
    {
        echo $E->getMessage();
    
    }
    
    echo '<br>' . $C;
?>

Output:

PHP Fatal error:  Uncaught Exception: Divide By Zero in /home/main.php:8
Stack trace:
#0 {main}
  thrown in /home/main.php on line 8

Explanation:

The above program will generate the error because we cannot re-throw the same exception.

Question 5:

<?php
    $A = 20;
    $B = 0;
    $C = 0;
    
    try
    {
        if ($B == 0) throw new Exception("Divide By Zero");
        $C = $A / $B;
    }
    catch(Exception $E)
    {
        throw new Exception("#Divide By Zero");
    }
    catch(Exception $E)
    {
        echo $E->getMessage();
    
    }
    
    echo '<br>' . $C;
?>

Output:

PHP Fatal error:  Uncaught Exception: #Divide By Zero in /home/main.php:13
Stack trace:
#0 {main}
  thrown in /home/main.php on line 13

Explanation:

The above program will generate an error because we cannot re-throw the same exception.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.