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;
?>
Answer 1:
Output:
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:
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:
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:
Explanation:
The above program will generate the error because we cannot re-throw the same exception.
Answer 5:
Output:
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