简体   繁体   中英

What is the difference between the following try-catch statements?

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();
}
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.

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.

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.

创建条件语句时的概念相同,只是条件语句正在测试条件,而try catch正在测试错误

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM