Exception checked or discarded

Possible duplicate:
When to select marked and unchecked exceptions

Hello!

So, I still like to throw a checked or unchecked exception. I would like to know what others consider 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 an exception at runtime, because I cannot easily restore the situation when the user passes bad data. I would like to indicate in advance that I do not control the transmitted data. If I could, I would create an interface that ensures that the condition in the constructor is true. However, this is a convenient class for correlations that have already been calculated, so I have to trust that the user provides accurate information.

, , !

+3
3

, . , , , . java-, IllegalArgumentException, .

Joshua Block . , , , - , . , . , .

2 .


Edit

, - 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.>
+6

-, :

  • , ?
  • API ?

- , . .

+4

. :

    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);

, , stacktrace , . ONE, , , . , , .

+2

Source: https://habr.com/ru/post/1714258/


All Articles