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

查看:70
本文介绍了Java if 与 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这两种方法有没有性能差异?

此外,使用 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 个参数,快速且确定性地失败如果任何此类参数无效"(例如,null 或空字符串),则不会影响其余代码.

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 用于 null 或空字符串),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 与 try/catch 开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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