Java if vs. try/catch 开销 [英] Java if vs. try/catch overhead

查看:33
本文介绍了Java if vs. try/catch 开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 中使用 try/catch 块而不是 if 块 是否有任何开销(假设所附代码不要求这样做)?

Is there any overhead in Java for using a try/catch block, as opposed to an if block (assuming that the enclosed code otherwise does not request so)?

以下面两个简单的字符串安全修剪"方法实现为例:

For example, take the following two simple implementations of a "safe trim" method for strings:

public String tryTrim(String raw) {
    try {
        return raw.trim();
    } catch (Exception e) {
    }
    return null;
}

public String ifTrim(String raw) {
    if (raw == null) {
        return null;
    }
    return raw.trim();
}

如果raw输入很少null,两种方法性能上有区别吗?

If the raw input is only rarely null, is there any performance difference between the two methods?

此外,使用 tryTrim() 方法来简化代码布局是否是一种很好的编程模式,尤其是在许多 if 块> 将代码包含在一个 try/catch 块中可以避免检查罕见的错误条件吗?

Furthermore, is it a good programming pattern to use the tryTrim() approach for simplifying the layout of code, especially when many if blocks checking rare error conditions can be avoided by enclosing the code in one try/catch block?

例如,有一个带有 N 个参数的方法是一种常见的情况,它在接近开始时使用 M <= N 个参数,快速且确定性地失败如果任何此类参数无效"(例如,空字符串或空字符串),则不会影响代码的其余部分.

For example, it is a common case to have a method with N parameters, which uses M <= N of them near its start, failing quickly and deterministically if any such parameter is "invalid" (e.g., a null or empty string), without affecting the rest of the code.

在这种情况下,不必编写 k * M if blocks(其中 k 是每个参数的平均检查次数,例如 k = 2 对于空字符串或空字符串),try/catch 块会显着缩短代码,并且可以使用 1-2 行注释来明确指出非常规"逻辑.

In such cases, instead of having to write k * M if blocks (where k is the average number of checks per parameter, e.g. k = 2 for null or empty strings), a try/catch block would significantly shorten the code and a 1-2 line comment could be used to explicitly note the "unconventional" logic.

这样的模式还可以加快方法的速度,尤其是在错误条件很少发生的情况下,并且不会影响程序安全性(假设错误条件是正常的",例如在字符串处理方法中,其中 null或空值是可以接受的,尽管很少出现).

Such a pattern would also speed up the method, especially if the error conditions occur rarely, and it would do so without compromising program safety (assuming that the error conditions are "normal", e.g. as in a string processing method where null or empty values are acceptable, albeit seldom in presence).

推荐答案

我知道你在问性能开销,但你真的不应该使用 try/catchif 可以互换.

I know you're asking about performance overhead, but you really should not use try/catch and if interchangeably.

try/catch 用于处理超出您控制范围且不在正常程序流程中的错误.例如,尝试写入文件并且文件系统已满?这种情况通常应该用 try/catch 处理.

try/catch is for things that go wrong that are outside of your control and not in the normal program flow. For example, trying to write to a file and the file system is full? That situation should typically be handled with try/catch.

if 语句应该是正常的流程和普通的错误检查.因此,例如,用户无法填充必需的输入字段?为此使用 if,而不是 try/catch.

if statements should be normal flow and ordinary error checking. So, for example, user fails to populate a required input field? Use if for that, not try/catch.

在我看来,您的示例代码强烈建议正确的方法是使用 if 语句而不是 try/catch.

It seems to me that your example code strongly suggests that the correct approach there is an if statement and not a try/catch.

为了回答您的问题,我推测 try/catch 通常比 if 有更多的开销.要确定,请获取 Java 分析器并找出您关心的特定代码.答案可能因情况而异.

To answer your question, I would surmise that there is generally more overhead in a try/catch than an if. To know for sure, get a Java profiler and find out for the specific code you care about. It's possible that the answer may vary depending on the situation.

这篇关于Java if vs. try/catch 开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆