Sunday 27 January 2013

PHP Exception Handling


<?php
//create function with an exception
function myNumber($a,$b)
{
if ($a > $b)
{
echo"After throwing exception from catch block we are inside the myNumber funtion";
throw new Exception("Value of $a is greater than $b");
}
}
//trigger exception inside the try block
try
{
echo "Inside the try block <br/>";
myNumber(22,2);
}
//catch the exception inside the catch block
catch(Exception $e)
{
echo "<br/>";
echo"Inside the catch block <br/>";
echo $e->getMessage();
}
?>

Output

Inside the try block
After throwing exception from catch block we are inside the myNumber funtion
Inside the catch block
Value of 22 is greater than 2

No comments:

Post a Comment