检查或未检查的异常

发布于 2024-08-01 15:41:00 字数 971 浏览 7 评论 0原文

可能的重复:
何时选择检查和非检查异常

您好!

因此,对于何时抛出已检查或未检查的异常,我仍然感到满意。 我想知道其他人认为在这种情况下最合适的是什么:

class Correlation<T>
{
    private final T object1, object2;
    private final double correlationCoefficient;

    public Correlation(T object1, T object2, double correlationCoefficient)
    {
        if(Math.abs(correlationCoefficient) > 1.0 || (object1.equals(object2) && correlationCoefficient != 1.0))
            throw new IllegalArgumentException();

        this.object1 = object1;
        this.object2 = object2;
        this.correlationCoefficient = correlationCoefficient;
    }
}

因此,在这种情况下,我想抛出一个运行时异常,因为我无法轻松地从用户传递错误数据的情况中恢复。 我想事先指出,我无法控制传入的数据。如果可以的话,我会创建一个接口来保证构造函数中的条件为 true。 然而,这是一个已经计算出的相关性的便利类,因此我必须相信用户正在提供准确的信息。

好吧,让我知道你们的想法!

Possible Duplicate:
When to choose checked and unchecked exceptions

Hello!

So, I'm still getting comfortable regarding when to throw a checked or unchecked exception. I would like to know what others think is the most appropriate in this case:

class Correlation<T>
{
    private final T object1, object2;
    private final double correlationCoefficient;

    public Correlation(T object1, T object2, double correlationCoefficient)
    {
        if(Math.abs(correlationCoefficient) > 1.0 || (object1.equals(object2) && correlationCoefficient != 1.0))
            throw new IllegalArgumentException();

        this.object1 = object1;
        this.object2 = object2;
        this.correlationCoefficient = correlationCoefficient;
    }
}

So, in this case, I would like to throw a runtime exception because I cannot easily recover from a situation where the user passes in bad data. I would like to point out beforehand that I have no control over the data being passed in. If I could, I would create an interface which guarantees that the conditional in the constructor is true. However, this is a convenience class for correlations that have already been computed, so I have to trust that the user is providing accurate information.

Okay, let me know what you all think!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

说好的呢 2024-08-08 15:41:00

我认为这是正确的回应。 您正在有效地执行屏障断言,即屏障检查,如果它们错误,您将拒绝创建实体。 我会用 java 文档记录您可以抛出 IllegalArgumentException,但除此之外,它看起来是正确的。

Joshua Block 有一些关于检查和非检查异常的重要信息。 基本前提是,除非您绝对希望有人检查异常,否则您应该抛出未经检查的异常。 以这种方式思考可能会使一些编码和返回值变得复杂,但总的来说,它可以使代码更清晰、更高效。 在特殊情况下使用例外,事情会更适合你。

只是我的2分钱。


编辑

需要明确的是,这里有一些类似于您应该拥有的 java 文档:

/**
 * <Something describing constructor, and what it does, ending with a period.>
 *
 * @param parameter <Describe the parameter - do one for each parameter of the constructor,
 *     and note which values may be illegal for that particular parameter.>
 * @throws IllegalArgumentException <the case for the illegal argument exception.>

I think this is the correct response. You are effectively doing barrier asserts, i.e. barrier checks, and if they are wrong you are refusing to create the entity. I would document with a java doc that you can throw an IllegalArgumentException, however outside of that, it looks correct.

Joshua Block has some great information regarding checked and unchecked exceptions. The basic premise is that, unless you absolutely want someone checking for the exception, you should throw an unchecked exception. Thinking this way can complicate some coding and return values, but in general it makes for cleaner, more efficient code. Use exceptions for exceptional cases, and things will work better for you.

Just my 2 cents.


Edit

Just to be clear, here is something like the java doc you should have:

/**
 * <Something describing constructor, and what it does, ending with a period.>
 *
 * @param parameter <Describe the parameter - do one for each parameter of the constructor,
 *     and note which values may be illegal for that particular parameter.>
 * @throws IllegalArgumentException <the case for the illegal argument exception.>
七月上 2024-08-08 15:41:00

在我看来,答案取决于:

  • 您是否期望调用者能够正常恢复?
  • 该 API 是供公共使用还是内部使用?

有人会告诉你,你永远不应该使用受检查的异常。 这纯粹是主观的。

In my opinion, the answer hinges on:

  • Do you expect the caller to be able to recover gracefully?
  • Is this API intended for public or internal consumption?

Someone people will tell you that you should never use checked exceptions. That's purely subjective.

厌味 2024-08-08 15:41:00

您应该始终在例外情况中包含解释性文本。 在这种特殊情况下,您甚至可以考虑进行两次检查:

    if(Math.abs(correlationCoefficient) > 1.0)
            throw new IllegalArgumentException("abs(correlationCoefficient) > 1.0 - " + correlationCoefficient);
    if((object1.equals(object2) && correlationCoefficient != 1.0))
            throw new IllegalArgumentException("object1==object2, but correlationCoefficient != 1.0, " + correlationCoefficient);

这使得那些真正看到堆栈跟踪的人能够确定确切的原因,而无需仔细查看代码。 给定的异常只能由一个条件触发,而不是多个条件,因为您无法确定发生了什么。 还包括所有必要的信息,因为如果无法在测试场景中重现错误情况,这可能至关重要。

You should ALWAYS include an explanatory text in your exception. In this particular case you might even consider have TWO checks:

    if(Math.abs(correlationCoefficient) > 1.0)
            throw new IllegalArgumentException("abs(correlationCoefficient) > 1.0 - " + correlationCoefficient);
    if((object1.equals(object2) && correlationCoefficient != 1.0))
            throw new IllegalArgumentException("object1==object2, but correlationCoefficient != 1.0, " + correlationCoefficient);

This allows those who actually get to see the stacktrace to be able to identify the exact cause without having to look intimately at the code. A given exception should only be triggered by ONE condition, not several, since you will not be certain what happended. Also include all necessary information as this may be crucial if the error situation cannot be reproduced in a test scenario.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文