0

I am confused. Does it depend on the concrete situation? Or it has a universal standard? Could anyone give a summary to tell me when to use if-else, and when to use try...catch?

Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
guo
  • 9,674
  • 9
  • 41
  • 79
  • 1
    `try ... catch` will only be useful as a conditional when the statement you try will actually throw an exception. When this is the case, there is no way to detect this with an `if-else` since the if-else will be aborted completely when the exception occurs – Tibrogargan Jul 29 '16 at 05:26
  • 2
    `try-catch` is for exception handling. Not for condition check – Jens Jul 29 '16 at 05:26
  • Related reading: http://stackoverflow.com/questions/99683/which-and-why-do-you-prefer-exceptions-or-return-codes – Thilo Jul 29 '16 at 05:29
  • 1
    Possible duplicate of [Throws or try+catch](http://stackoverflow.com/questions/3203297/throws-or-trycatch) – tenten Jul 29 '16 at 06:05

4 Answers4

3

if else statements are for evaluating boolean expressions, for instance,

if(isWeekday){
    return "wakeup@6:00AM";
}
else{
    return "wakeup@10:00AM";

you would use an if else statement for data you know you have, and when you can easily compute a boolean expression. Some times, you aren't really sure what you're dealing with and could get errors trying to deal with it. In a case like this, you would use a try catch.

try{
    Scanner data = new Scanner(new File("data.dat"));
} catch (IOException e){
    e.printStackTrace();
}

In this scenario you can't really be sure that the file you want to get data from even exists, so therefore you can't perform any reasonable computation on it.

Funny Geeks
  • 483
  • 5
  • 12
2

I guess this is too wide question, but from my point of view try...catch block must be used only in exceptional cases, the ones that you are not really expecting to happen, however, the application could recover from these situations. If-else is a completely normal block to allow your program to go to two different directions.

Rufi
  • 2,529
  • 1
  • 20
  • 41
0

The using try-catch instead of if-else you really should not use them interchangeably.

try/catch clause is used for things that goes wrong that are outside of your control and not in the normal program flow. For example, trying to write to a file and the file system is full? That situation should typically be handled with try/catch.

if-else clause should be used in normal flow and ordinary error checking. So, for example, user fails to populate a required input field? Use if for that, not try/catch.

Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
0

In java, you use the try-catch statement when your code has a chance of throwing a exception which will need to be handed. The logic in the try-catchis ran until a exception is found where the logic moves to the catch box where you can handle the exception.

try{
    //Exception prone logic
}catch(Exception e){
    //handle the exception here
}

The if-else statement is a conditional statement which evaluates a boolean expression which directs the flow of the program either into the if-block of code or the else-block of code

if(i == 0){
    //i is equal to 0
}else{
    //i is not equal to 0
}
Ryan
  • 1,972
  • 2
  • 23
  • 36