I have this PHP code. Whenever y becomes zero, it shows a warning instead of catching the exception. Is there anything wrong with my code?
try
{
return($x % $y);
throw new Exception("Divide error..");
}
catch(Exception $e){
echo "Exception:".$e->getMessage();
}
I got this warning:
Warning: Division by zero in file.php
The catch block is not run. What am I doing wrong?
A warning is not an exception. Warnings cannot be caught with exception handling techniques. Your own exception is never thrown since you always
returnbefore.You can suppress warnings using the
@operator like@($x % $y), but what you should really do is make sure$ydoes not become 0.I.e.: