Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将ArgumentOutOfRangeException与多个参数一起使用?_C#_Exception - Fatal编程技术网

C# 如何将ArgumentOutOfRangeException与多个参数一起使用?

C# 如何将ArgumentOutOfRangeException与多个参数一起使用?,c#,exception,C#,Exception,我有以下代码 DoSomething(int min, int max) { if (min < 1 || min > 5) throw new ArgumentOutOfRangeException("min"); if (max < 1 || max > 5) throw new ArgumentOutOfRangeException("max"); if (min > max) throw

我有以下代码

DoSomething(int min, int max)
{
    if (min < 1 || min > 5)
        throw new ArgumentOutOfRangeException("min");
    if (max < 1 || max > 5)
        throw new ArgumentOutOfRangeException("max");
    if (min > max)
        throw new ArgumentOutOfRangeException("min & max");

    DoSomethingWithYourLife(); // =)
}
DoSomething(int-min,int-max)
{
如果(最小值<1 | |最小值>5)
抛出新ArgumentOutOfRangeException(“min”);
如果(最大值<1 | |最大值>5)
抛出新ArgumentOutOfRangeException(“max”);
如果(最小值>最大值)
抛出新ArgumentOutOfRangeException(“最小和最大”);
DoSomethingWithYourLife();/=)
}
在文档中,我声明min和max必须在[1-5]范围内,max必须大于或等于min


第三个异常构造是否正确?如果没有,我应该如何构造异常?

我强烈建议只选择其中一个,但提供一个更详细的错误消息,其中包含构造函数的名称:

if (min > max)
    throw new ArgumentOutOfRangeException("max", "max must be greater than or equal to min");

否,
ArgumentOutOfRangeException
构造函数的参数应始终是参数名称之一。您可以选择其中任何一个-我通常假设前面的参数是正确的,因此后面的参数相对于它来说是不正确的。不过,你可以(也应该)在信息中提供更多信息。如果您给出实际值,它也非常方便-因此:

if (min < 1 || min > 5)
{
    throw new ArgumentOutOfRangeException("min", min, "min must be between 1 and 5 inclusive");
}
if (max < 1 || max > 5)
{
    throw new ArgumentOutOfRangeException("max", max, "max must be between 1 and 5 inclusive");
}
if (max < min)
{
    throw new ArgumentOutOfRangeException("max", max, "max must not not be less than min");
}
if(最小值<1 | |最小值>5)
{
抛出新ArgumentOutOfRangeException(“min”,min,“min必须介于1和5之间(包括1和5));
}
如果(最大值<1 | |最大值>5)
{
抛出新ArgumentOutOfRangeException(“max”,max,“max必须介于1和5之间(包括1和5));
}
如果(最大值<最小值)
{
抛出新ArgumentOutOfRangeException(“max”,max,“max不得小于min”);
}
对于,我有用于此的帮助器方法,例如:

internal static void CheckArgumentRange(string paramName,
    int value, int minInclusive, int maxInclusive)
{
    if (value < minInclusive || value > maxInclusive)
    {
        throw new ArgumentOutOfRangeException(paramName, value,
            "Value should be in range [" + minInclusive + "-" + maxInclusive + "]");
    }
}
内部静态void CheckArgumentRange(字符串参数名,
int值、int minInclusive、int maxInclusive)
{
如果(值maxInclusive)
{
抛出新ArgumentOutOfRangeException(参数名、值、,
“值的范围应为[“+minInclusive+”-“+maxInclusive+”]”;
}
}
这样,您可以将上述内容简化为:

Preconditions.CheckArgumentRange("min", min, 1, 5);
Preconditions.CheckArgumentRange("max", max, 1, 5);
if (max < min)
{
    throw new ArgumentOutOfRangeException("max", max, "max must not not be less than min");
}
先决条件。检查参数范围(“min”,min,1,5);
先决条件。检查参数范围(“最大值”,最大值,1,5);
如果(最大值<最小值)
{
抛出新ArgumentOutOfRangeException(“max”,max,“max不得小于min”);
}