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

查看:133
本文介绍了哪个更好/更有效:检查错误的值或捕获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,那么答案不一样(其中更普遍的是询问是否可以假设检查值始终比依赖于异常处理更有效,因为它增加了当前编译器/解释器的更多开销。)

  • (New )抛出异常的行为很慢。进入try块是否有开销,即使没有抛出异常?

  • How does your answer change if the value is "bad" (would throw an exception) most of the time?
  • How much of this would depend on the specifics of the VM in use?
  • If this same question was asked for language-X, would the answer be different? (Which, more generally, is asking if it can be assumed checking values is always more efficient than relying on exception handling simply because it adds more overhead by current compilers/interpreters.)
  • (New) The act of throwing an exception is slow. Does entering a try block have overhead, even if an exception is not thrown?

与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天全站免登陆