简体   繁体   English

以下try-catch语句有什么区别?

[英]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-catch块用法和何时应使用每个块有什么区别?

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. doSomthing1()doSomthing2()是不相关的方法。 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. 如果doSomthing1()失败,我们可以使用try-catch块停止doSomthing2()方法的执行。 We can handle each exception individually with two catch blocks. 我们可以使用两个catch块分别处理每个异常。 But, one important point to note is that, your 2nd catch block is an unreachable code . 但是,需要注意的重要一点是,您的2nd catch blockunreachable code In general, you should have catch block for more specific exceptions first, followed by generalized exception. 通常,应该首先具有catch块以获取更具体的异常,然后是广义异常。 Now, in your case, all the exception that 2nd catch block is supposed to handle will already be handled in the first one. 现在,在您的情况下,第2个c​​atch块应该处理的所有异常都将在第一个中处理。

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

We have 2 try-catch blocks embedded in each other. 我们有2个try-catch块相互嵌入。 Even after the doSomthing2() fails the program will continue inside the try block. 即使在doSomthing2()失败之后,该程序仍将在try块内继续。

Well, the obvious difference between the first and the other two is that doSomthing2 will be attempted whether or not doSomthing1 threw an exception. 好吧,第一个和另外两个之间的明显区别是,无论doSomthing1是否引发异常, doSomthing2将尝试doSomthing1 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. 在您引用的确切代码中,除了第三个示例之外,第二个和第三个示例之间没有很大区别(除了语法错误),第二次try的异常处理代码在第一个示例的异常处理代码之 ,因此如果抛出该异常,该异常将被捕获。

Which you should use depends entirely on the situation. 您应该使用哪种完全取决于情况。 Sometimes, it's appropriate to run doSomthing2 whether or not doSomthing1 throws an exception. 有时,它是适合运行doSomthing2是否doSomthing1抛出异常。 Sometimes it isn't. 有时候不是。

If doSomThing1 fails then the code moves on to execute doSomthing2 如果doSomThing1失败,则代码继续执行doSomthing2

In the second example, doSomthing2 does not get executed if doSomthing1 fails 在第二个示例中,如果doSomthing1失败,则不会执行doSomthing2

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. 首先让我们假设doSomething1()和exceltion_handle1()不要调用System.exit(x)之类的东西。

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. 1)因此,第一段代码将执行doSomething1(),无论doSomething1()是否抛出任何异常,它都将对其进行处理(处理catch代码块),并进行第二次尝试并以相同的方式运行它。

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). 2)此代码将先运行doSomething1()然后再运行doSomething2(),无论哪个失败(抛出异常),都将仅调用第一个catch子句,因为它吸收了所有子类及其本身,因此不会捕获第二个catch达到(首先接受所有可能的异常)。 So afaik, you should get an error (shouldn't compile). 所以afaik,您应该会得到一个错误(不应编译)。 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. 这是合乎逻辑的,因为catch子句的顺序降低了,上部catch不应成为底部子句的父级,因为ANYWAY父级都会接受该异常,并且不会到达底部的子级。

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. 3)这一个:如果doSomething1()将失败,则将执行e1 catch子句,仅此而已,如果它将通过,则如果doSomething2()将运行,并且如果失败,则将执行e2 catch子句。

Notice @ second example, no matter which doSomething will fail, e1 will be executed (don't forget there is an error as second is unreachable). 请注意第二个示例,无论哪个doSomething失败,都将执行e1(不要忘记出现错误,因为第二个错误无法到达)。 But I understand what you wanted to ask. 但我知道您想问什么。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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