哪个更好/更有效:在 Java 中检查错误值或捕获异常 [英] Which is better/more efficient: check for bad values or catch Exceptions in Java

查看:23
本文介绍了哪个更好/更有效:在 Java 中检查错误值或捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 中哪个更有效:检查错误值以防止异常还是让异常发生并捕获它们?

Which is more efficient in Java: to check for bad values to prevent exceptions or let the exceptions happen and catch them?

这里有两个示例代码块来说明这种差异:

Here are two blocks of sample code to illustrate this difference:

void doSomething(type value1) {
    ResultType result = genericError;

     if (value1 == badvalue || value1 == badvalue2 || ...) {
          result = specificError;
     } else {
          DoSomeActionThatFailsIfValue1IsBad(value1);
          // ...
          result = success;
     }
     callback(result);
}

对比

void doSomething(type value1) {
     ResultType result = genericError;
     try {
          DoSomeActionThatFailsIfValue1IsBad(value1);
          // ...
          result = success;
     } catch (ExceptionType e) {
          result = specificError;
     } finally {
          callback(result);
     }
}

一方面,您总是在进行比较.另一方面,老实说,我不知道系统内部做了什么来生成异常、抛出异常,然后触发 catch 子句.它听起来效率较低,但如果它在非错误情况下不增加开销,那么平均来说它的效率更高.是哪个?它是否添加了类似的检查?是否在为异常处理添加的隐式代码中进行检查,即使附加了显式检查层?也许它总是取决于异常的类型?我没有考虑什么?

On the one hand, you're always doing a comparison. On the other hand, I honestly don't know what the internals of the system do to generate an exception, throw it, and then trigger the catch clause. It has the sound of being less efficient, but if it doesn't add overhead in the non-error case, then it's more efficient, on average. Which is it? Does it add similar checking anyway? Is that checking there in the implicit code added for exception handling, even with the additional layer of explicit checking? Perhaps it always depends on the type of exception? What am I not considering?

我们还假设所有坏值"都是已知的——这是一个明显的问题.如果您不知道所有的错误值——或者列表太长且不规则——那么无论如何,异常处理可能是唯一的方法.

Let's also assume that all "bad values" are known -- that's an obvious issue. If you don't know all the bad values -- or the list is too long and not regular -- then exception handling may be the only way, anyway.

那么,每种方法的优缺点是什么,为什么?

So, what are the pros and cons of each, and why?

要考虑的附带问题:

  • 如果大多数时候值是坏的"(会抛出异常),你的答案会如何变化?
  • 这在多大程度上取决于所用虚拟机的具体情况?
  • 如果对语言 X 提出同样的问题,答案会有所不同吗?(更一般地说,这是询问是否可以假设检查值总是比依赖异常处理更有效,因为它增加了当前编译器/解释器的更多开销.)
  • (新)抛出异常的行为很慢.即使没有抛出异常,进入 try 块也有开销吗?

SO 的相似之处:

  • 这类似于这个答案中的代码示例,但声明它们仅在概念上相似,而不是编译后的实际情况.
  • 前提类似于这个问题但是,就我而言,任务的请求者(例如Something")不是方法的调用者(例如doSomething")(因此没有返回).
  • 这个非常相似,但是我没有找到我的问题的答案.

  • This is similar to the code sample in this answer, but states they are similar only in concept, not compiled reality.
  • The premise is similar to this question but, in my case, the requester of the task (e.g. "Something") isn't the caller of the method (e.g. "doSomething") (thus no returns).
  • And this one is very similar, but I didn't find an answer to my question.

与太多其他问题类似,除了:

And similar to far too many other questions to list, except:

我不是在问理论上的最佳实践.我问了更多关于运行时性能和效率的问题(这应该意味着,对于特定情况,没有意见答案),尤其是在资源有限的平台上.例如,如果唯一的错误值只是一个空对象,那么检查它或尝试使用它并捕获异常会更好/更有效吗?

I'm not asking about theoretical best practice. I'm asking more about runtime performance and efficiency (which should mean, for specific cases, there are non-opinion answers), especially on resource limited platforms. For instance, if the only bad value was simply a null object, would it be better/more efficient to check for that or just attempt to use it and catch the exception?

推荐答案

"如果大部分时间值为坏"(会抛出异常),你的答案会如何变化?"认为这是关键.与比较相比,异常的开销很大,因此您确实希望将异常用于异常 条件.

"How does your answer change if the value is "bad" (would throw an exception) most of the time?" I think that's the key right there. Exceptions are expensive as compared to comparisons, so you really want to use exceptions for exceptional conditions.

同样,您关于此答案可能如何根据语言/环境而变化的问题与此相关:在不同的环境中,异常的费用是不同的.例如,.Net 1.1 和 2.0 在第一次抛出异常时非常慢.

Similarly, your question about how this answer might change depending on the language/environment ties into that: The expense of exceptions is different in different environments. .Net 1.1 and 2.0 are incredibly slow the first time an exception is thrown, for instance.

这篇关于哪个更好/更有效:在 Java 中检查错误值或捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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