可能重复:
When to choose checked and unchecked exceptions经过或未经检查的异常
您好!
所以,我仍然很清楚什么时候抛出checked或unchecked异常。我想知道别人怎么想是最合适在这种情况下:
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;
}
}
所以,在这种情况下,我想抛出一个运行时异常,因为我不能轻易从用户传递的情况中恢复坏数据。我想事先指出我无法控制传入的数据。如果可以,我会创建一个接口,以确保构造函数中的条件为真。但是,这是已经计算出的相关性的便利类,所以我必须相信用户正在提供准确的信息。
好吧,让我知道你们都在想什么!
我会指导您http://stackoverflow.com/questions/27578/when-to-choose-checked-and-unchecked-exceptions – 2009-08-03 20:40:05
您使用IllegalArgumentException(运行时异常)是有效的,因为参数是非法的(根据构造函数的期望(应该在JavaDocs中记录))。 – 2009-08-03 21:13:51