Q:

PHP find output programs (Exceptions) | set 2

belongs to collection: PHP Find Output Programs

0

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.

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;
?>

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;
?>

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;
?>

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;
?>

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;
?>

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer 1:

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.

Answer 2:

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.

Answer 3:

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.

Answer 4:

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.

Answer 5:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Find Output Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
PHP find output programs (Filters) | set 1... >>
<< PHP find output programs (Exceptions) | set 1...