简体   繁体   English

try-catch 和循环在 Java 中的例外

[英]Exception with try-catch and loop in Java

In Java, what is the difference (in term of performance ) between:在 Java 中,有什么区别(在性能方面):

for (int i = 0; i < count; i++) {
    try {
        // code that throws Exception
    } catch (Exception e) {
        e.printStackTrace();
    }
}

and

try {
    for (int i = 0; i < count; i++) {
        // code that throws Exception
    }
} catch (Exception e) {
    e.printStackTrace();
}

In your first version the loop continues if it hits an exception, in the second version the loop continues after the catch block.在您的第一个版本中,如果遇到异常,循环将继续,在第二个版本中,循环在 catch 块之后继续。 That is the most important difference of those code snippets.这是这些代码片段最重要的区别。

You can use both, but it all depends on what you want it to do.您可以同时使用两者,但这完全取决于您想要它做什么。 If you want to continue the execution after the loop finishes once then you do it the first way.如果您想在循环完成一次后继续执行,那么您可以按照第一种方式执行。 If you want to catch an exception then stop executing the loop then you do the second.如果你想捕获一个异常然后停止执行循环然后你做第二个。 Performance wise it all depends on what you want to do with it.性能方面,这一切都取决于你想用它做什么。

Since you asked about performance in the two versions of code, I am reminded of "Practical Java" (Addison-Wesley 2000) which recommends in Praxis 23: Place try/catch blocks outside of loops .由于您询问了两个版本代码的性能,我想起了 Praxis 23 中推荐的“Practical Java”(Addison-Wesley 2000): Place try/catch blocks outside of loops

The reason involves running the code on a JVM with the JIT compiler turned off.原因涉及在关闭 JIT 编译器的情况下在 JVM 上运行代码。 In that case, the lack of run-time JIT optimizations results in extra branches in the opcode, leading to reduced performance.在这种情况下,缺乏运行时 JIT 优化会导致操作码中出现额外的分支,从而导致性能下降。

You can read JIT docs of 2014 here: http://www.oracle.com/technetwork/articles/java/architect-evans-pt1-2266278.html您可以在此处阅读 2014 年的 JIT 文档: http://www.oracle.com/technetwork/articles/java/architect-evans-pt1-2266278.ZFC35FZ730D5FC69D2E362

The main difference is that in the first snippet of code, even if there is an exception thrown from the try block and it is caught execution of the for loop continues.主要区别在于,在第一个代码片段中,即使 try 块抛出异常并且被捕获,for 循环的执行也会继续。 In the second snippet if an exception is thrown then the for loop is exited.在第二个片段中,如果抛出异常,则退出 for 循环。 This is because the whole loop is within the try block.这是因为整个循环都在 try 块内。

No I'm quite sure there's absolutely no difference from a point of performance here (ignoring the obvious fact about the loop).不,我很确定这里的性能绝对没有区别(忽略关于循环的明显事实)。 In both cases you create exactly one entry in the exception table - only the PC values (ie in which range the exception is valid) will be a bit different.在这两种情况下,您只在异常表中创建了一个条目——只有 PC 值(即异常在哪个范围内有效)会有点不同。

ie if you assume the following is the exception table the only thing that'll change are the x, y and z values..即,如果您假设以下是异常表,那么唯一会改变的是 x、y 和 z 值。

Exception table:
   from to target type
     x y z <Class java.lang.Exception>

Besides the difference in your logic with the continuing for.除了你的逻辑与持续的差异之外。 It's no noticeable difference between之间没有明显区别

try {
lots of stuff which might not throw any exception
something that throws exception
} catch (Exception e) {
}

and

lots of stuff which might not throw any exception
try {
something that throws exception
} catch (Exception e) {
}

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

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