Java 中的 try-catch 和循环异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6324012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:22:30  来源:igfitidea点击:

Exception with try-catch and loop in Java

javaexceptionfor-loop

提问by Martin08

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

采纳答案by RMT

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.

您可以同时使用两者,但这完全取决于您想要它做什么。如果您想在循环结束后继续执行,那么您可以使用第一种方式。如果你想捕获一个异常然后停止执行循环然后你做第二个。性能明智这一切都取决于你想用它做什么。

回答by driis

In your first version the loop continues if it hits an exception, in the second version the loop continues after the catch block. That is the most important difference of those code snippets.

在您的第一个版本中,如果遇到异常,循环将继续,在第二个版本中,循环在 catch 块之后继续。这是这些代码片段最重要的区别。

回答by preethinarayan

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. In the second snippet if an exception is thrown then the for loop is exited. This is because the whole loop is within the try block.

主要区别在于,在第一段代码中,即使有从 try 块中抛出的异常并被捕获,for 循环的执行也会继续。在第二个代码段中,如果抛出异常,则退出 for 循环。这是因为整个循环都在 try 块内。

回答by Voo

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>

回答by J Slick

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.

由于您询问了两个版本代码的性能,我想起了“实用 Java”(Addison-Wesley 2000),它在 Praxis 23: Place try/catch blocks outside of loops 中推荐

The reason involves running the code on a JVM with the JIT compiler turned off. In that case, the lack of run-time JIT optimizations results in extra branches in the opcode, leading to reduced performance.

原因涉及在关闭 JIT 编译器的情况下在 JVM 上运行代码。在这种情况下,缺乏运行时 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.html

回答by Cosmin Cosmin

Besides the difference in your logic with the continuing for. It's no noticeable difference between

除了您的逻辑与继续 for 的差异之外。两者之间没有明显区别

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) {
}