I am new to php, i am from java background, i am wondering why php doesn't goes directly when exception occured in try block without throwing that exception manually. e.g.
<?php
//create function with an exception
function checkNum($number) {
if($number/0) {
throw new Exception("Value must be 1 or below");
}
return true;
}
//trigger exception in a "try" block
try {
checkNum(2);
//If the exception is thrown, this text will not be shown
echo 'If you see this, the number is 1 or below';
}
//catch exception
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
}
?>
in above example in if condition the divide by zero exception is occured and then it will directly go to the catch block instead it goes to inside if.why?
if()block.$number/0returnsfalsebecause of the division by 0, so it doesn't execute theif(). Then the function returnstrue, and it prints theIf you see thismessage.