PHP Tutorial

PHP Examples

PHP Practice

PHP find output programs (Exceptions) | set 1

Find the output of PHP programs | Exceptions | Set 1: 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
    function fun()
    {
        $A = printf("Hello World");
    
        try
        {
            if ($A > 10) throw new Exception("Value must be below 10");
        }
    }
    
    fun();
?>

Output:

PHP Fatal error:  Cannot use try without catch or finally 
in /home/main.php on line 7

Explanation:

The program will generate syntax error because we cannot try block without catch or finally block in PHP.

Question 2:

<?php
    function fun()
    {
        $A = printf("Hello World");
        try
        {
            if ($A > 10) throw new Exception("Value must be below 10");
        }
        finally
        {
            echo "<br>finally block executed";
        }
    }
    
    fun();
?>

Output:

Hello World
finally block executed

Explanation:

In the above program, we created a variable $A initialized with the value returned by printf() function. Here, printf() function returns total number of characters printed by printf(). In the above code printf() function will print "Hello World" and returns 11, which is assigned to variable $A.

Here, we defined try and finally blocks, within try block we checked if condition, then the condition will true, and throw an exception, but we did not define catch block. Then the exception is not handled. But, then finally block will execute and print "finally block executed".

Question 3:

<?php
    function fun()
    {
        $A = printf("Hello World");
        try
        {
            if ($A > 10) throw new Exception("Value must be below 10");
        }
        catch(Exception $E)
        {
            echo "<br>$E";
        }
        finally
        {
            echo "<br>finally block executed";
        }
    }

    fun();
?>

Output:

Hello World
Exception: Value must be below 10 in /home/main.php:7
Stack trace:
#0 /home/main.php(19): fun()
#1 {main}
finally block executed

Explanation:

In the above program, we created a variable $A initialized with the value returned by printf() function. Here, printf() function returns total number of characters printed by printf(). In the above code printf() function will print "Hello World" and returns 11, which is assigned to variable $A.

Here, we defined try, catch, and finally blocks, within try block we checked if condition, then the condition will true, and throw an exception, Then catch block will catch the exception, then we print $E directly then it will show thrown exception as well as some internal trace data, and then finally block will execute at the end and print "finally block executed" on the web page.

Note: To print proper messages caught by the catch block, we need to use the below statement.

echo $E->getMessage();

Question 4:

<?php
    function divide($A, $B)
    {
        try
        {
            $C = $A / $B;
        }
        catch(Exception $E)
        {
            echo "<br>" . $E->getMessage();
        }
        finally
        {
            echo "<br>finally block executed";
        }
    
        return $C;
    }

    $RES = divide(10, 0);

    echo '<br>' . $RES;
?>

Output:

finally block executed
INF

Explanation:

In the above program, we defined a function divide() that contains two arguments $A and $B they initialized with 10 and 0 respectively by the function call.

$C = $A/$B;

The above statement will generate a divide by zero exception, to handle this, we need to throw an exception manually. That's why the catch block is not called and then finally block executed.

Then, finally $RES will print INF on the webpage.

Question 5:

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

    $RES = divide(10, 0);

    echo '<br>' . $RES;
?>

Output:

Divide By Zero
0

Explanation:

In the above program, we defined a function divide() that contains two arguments $A and $B they initialized with 10 and 0 respectively by the function call.

$C = $A/$B;

The above statement will generate a divide by zero exception, here we check the value of $B, and here the value of $B is 0 then the exception will be thrown and will be caught by catch block and print "Divide By Zero" and return 0 from function divide(). Then finally the value of $RES that is 0 will be printed on the webpage.





Comments and Discussions!

Load comments ↻





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