1

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??

Pratik Joshi
  • 11,485
  • 7
  • 41
  • 73

4 Answers4

3

The use of exceptions (not only in PHP but in all languages with exceptions management) is not mandatory but is highly recommended.

Oracle guys have an excellent post with some of the main advantages of exceptions (it is for Java, but is exactly the same idea).

In summary:

  1. Separating Error-Handling Code from "Regular" Code
  2. Propagating Errors Up the Call Stack
  3. Grouping and Differentiating Error Types

Personally, I use exceptions just for convention, especially in big projects when the code gets difficult to read.

Of course you can do the same without it (only with if-else statements) but I like it because it provides semantic to my codes, separating the "normal or expected behavior" from "exceptional cases".

In addition, when you have too many conditions to check, if you use if-else, you will have too many nesting levels as well, making it difficult to read and understand.

In your specific example, if I only wanted to check division by 0, and no other programmer is going to use my function, probably I would use if-else. But if I were in a company, surely I would use exceptions.

nanocv
  • 2,227
  • 2
  • 14
  • 27
1

If you're asking about performance overhead,you really should not use try/catch. try/catch is for things that go wrong that are outside of your control and not in the normal program flow.If you can create logic to handle exception then always go for if/else as its less processing overhead. In you codes above, correct approach is one with if statement and not a try/catch. Cited

Community
  • 1
  • 1
BASEER ULHASSAN
  • 461
  • 4
  • 22
0

In case of run-time exceptions,you need to handle it so that the program or application doesn't stop running if an error occurs. If you don't handle it,the application/program will throw an error and stop running.You don't want this to happen if you are a developer who is creating and distributing a premium software. If such a thing happens,people or ordinary users would consider it as a bug in the software and move ahead for a better software.

So it is always better to handle exceptions.If you are using php to write some important script or program,I would suggest that you handle the exceptions.

Mathews Mathai
  • 1,707
  • 13
  • 31
0

I see what you mean, you are essentially asking why should you bother with exception handling when you can easily do the same with conditional checks? In the above example you are right, I do not see any particular need to use exception handling but I will give you a scenario where it is required :

Objective: divide two numbers

It is very easy and efficient to use if else for zero based division but note that it is an anticipated error condition meaning you already know about it before it even occurs.

Objective : read numbers from a file and divide them

Now the situation has changed. You can still use your division function but how can you guarantee that the file you are trying to open is available? or the disk the file is on , is still connected? or a million other things. Thus you can see why using if/else in such condition would most likely break your application unless you anticipate and account for all these different possibilities.

As a rule of thumb:

Use exceptions only for exceptional conditions

thepiyush13
  • 1,321
  • 1
  • 8
  • 9