Why use Exception if you can use If Else code instead Everywhere?What if we dont use Exception anywhere?
Just see if I dont want to use exception and use If Else simple conditions everywhere , it gets my work done with ease and with No doubt , so why use exception anywhere in my code ??
For example : Code with exception
<?php
function division($num1, $num2){
//If num == 0 , throw exc
if(!$num1){
throw new Exception("Hell no !! This is Divide by 0 Exception");
}
return $num2/$num1;
}
try{
echo "Division result is : ".division(0,22);
}catch (Exception $e){
echo 'Exception msg : '.$e->getMessage();
}
But say if I dont want to follow exception handling way using try catch etc.
Without exception
function division($num1, $num2){
$arr = array('error' => 0,'msg'=>'');
//If num == 0 , throw exc
if(!$num1){
$arr['error'] = 1;
$arr['msg'] = 'div by 0';
}
return $arr['result'] = $num2/$num1;
}
$res = division(0,22);
if($res['error']) {
echo "errr div by 0";
}else{
echo $res['result'];
}
If we simply use If Else conditions and Exit(0) Or Return with some custom message and its code , it will still work as expected. So Why to use exceptions in code ? And what if I dont use exception and use conditional code with return custom message??