-1

What is the difference between these usage of try-catch blocks and when you should use each one?

try {
  doSomething1();
} catch(Exception e1){
  exception_handle1();
}
try {
  doSomething2();
} catch(Exception e2){
  exception_handle2();
}

try {
  doSomething1();
  doSomething2();
} catch(Exception e1) {
  exception_handle1();
} catch(Exception e2) {
  exception_handle2();
}

try {
  doSomething1();
  try {
    doSomething2();
  } catch(Exception e2){
    exception_handle2();
  }
} catch(Exception e1){
  exception_handle1();
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ari M
  • 1,386
  • 3
  • 18
  • 33
  • 1
    Quite amazingly, all of them are syntax errors. – melpomene Dec 03 '12 at 08:31
  • The first is very similar to the second one if you're looking for an Exception in general. Unless you make the Exceptions more specific (such as IOException, MathException, etc) then they are almost the same. – hesson Dec 03 '12 at 08:34
  • what kind of a question is this! Try yourself in Eclipse! – Narendra Pathai Dec 03 '12 at 08:37
  • The third case is slightly different from second case - if you consider subclass of Exception (not the java.lang.Exception). Actually, in 2nd case, the first catch will catch all Exception and the 2nd handler will never be called. – nhahtdh Dec 03 '12 at 08:44

5 Answers5

3
try {
  doSomthing1()
catch(Exception e1){
  exception_handle1()
}
try {
  doSomthing2()
catch(Exception e2){
  exception_handle2()
}

doSomthing1() and doSomthing2() are unrelated methods. Failure of either one of them is independent on each other.

try {
  doSomthing1()
  doSomthing2()
catch(Exception e1){
  exception_handle1()
}catch(Exception e2){
  exception_handle2()
}

We can use the try-catch block this way to stop doSomthing2() method from executing if doSomthing1() fails. We can handle each exception individually with two catch blocks. But, one important point to note is that, your 2nd catch block is an unreachable code. In general, you should have catch block for more specific exceptions first, followed by generalized exception. Now, in your case, all the exception that 2nd catch block is supposed to handle will already be handled in the first one.

try {
  doSomthing1()
  try {
    doSomthing2()
  catch(Exception e2){
    exception_handle2()
  }
}
catch(Exception e1){
  exception_handle1()
}

We have 2 try-catch blocks embedded in each other. Even after the doSomthing2() fails the program will continue inside the try block.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Dan Ciborowski - MSFT
  • 6,807
  • 10
  • 53
  • 88
2

Well, the obvious difference between the first and the other two is that doSomthing2 will be attempted whether or not doSomthing1 threw an exception. In the exact code you quoted, there isn't a huge difference between the second and third examples (syntax errors aside) other than that in the third example, your exception handling code for the second try is within the exception handling code for the first, and so if it throws, the throw will be caught.

Which you should use depends entirely on the situation. Sometimes, it's appropriate to run doSomthing2 whether or not doSomthing1 throws an exception. Sometimes it isn't.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Did you mean `doSomething2` will be executed in the first code? Actually your first statement is confusing. You might want to update it. – Rohit Jain Dec 03 '12 at 08:36
  • @RohitJain: Yes, in the first example, `doSomthing2` will be executed regardless of whether `doSomthing1` threw; in the second and third, it won't. What's confusing about my first statement? (I had a typo in it that I corrected a moment ago, maybe that was it?) – T.J. Crowder Dec 03 '12 at 08:38
  • Oh Yeah. That was it. Didn't refreshed the page. My bad. – Rohit Jain Dec 03 '12 at 08:39
  • 1
    @RohitJain: Or mine for the typing error in the first place. :-) – T.J. Crowder Dec 03 '12 at 08:40
2

If doSomThing1 fails then the code moves on to execute doSomthing2

In the second example, doSomthing2 does not get executed if doSomthing1 fails

Whereas, third example is similar to second one.

java_dude
  • 4,038
  • 9
  • 36
  • 61
0

its the same concept when you creating a conditionaL statement, just that conditionaL statement is testing the condition the try catch is testing an error

0

First lets asume, doSomething1() and exceltion_handle1(), don't call System.exit(x) or something.

1) So first piece of code, will doSomething1(), no matter doSomething1() will throw any Exception or not, it will handle it (process the catch code block) and advance to second try and run it the same way.

try {
  doSomething1();
} catch(Exception e1){
  exception_handle1();
}
try {
  doSomething2();
} catch(Exception e2){
  exception_handle2();
}

It's morning, so I hope I won't make any wrong decisions.

2) This code will run doSomething1() then doSomething2(), and no matter which one will fail (throw Exception), only first catch clause will be called, as it absorbs all the subclasses and itself, so second catch won't be reached (first takes all the possible exceptions). So afaik, you should get an error (shouldn't compile). It's smart and will recognize, that second catch won't be reached in any way.

The correct pattern would be : as we go to the bottom, exceptions should be broader and broader (strictly). It's logical, as order of catching clauses goes down, upper catch shouldn't be parent of bottom ones, as ANYWAY parent will take that exception, and childs in the bottom won't be reached.

Example: (I recommend you to learn about Multicatch in java.)
catch (Specific2ChildOfSpecific1 e3)
...
catch (specific1ChildOfException e2)
...
catch (Exception e1)


try {
  doSomething1();
  doSomething2();
} catch(Exception e1) {
  exception_handle1();
} catch(Exception e2) {
  exception_handle2();
}

3) This one: If doSomething1() will fail e1 catch clause will be executed and thats all, if it will pass, then if doSomething2() will run and if it fails, then e2 catch clause will be executed.

Notice @ second example, no matter which doSomething will fail, e1 will be executed (don't forget there is an error as second is unreachable). But I understand what you wanted to ask.

try {
  doSomething1();
  try {
    doSomething2();
  } catch(Exception e2){
    exception_handle2();
  }
} catch(Exception e1){
  exception_handle1();
}
EEE
  • 61
  • 1
  • 6