尝试将重复项添加到集合时应该抛出什么异常类型?

时间:2022-01-18 20:34:43

Following code should throw exception to prevent adding duplicate collection item.

以下代码应抛出异常以防止添加重复的集合项。

ICollection<T> collection = new List<T>();

public void Add(T item)
{
    if (collection.Contain(item))
    {
          throw new SomeExceptoinType()
    }

    collection.Add(item);
}

What standard exception type is the most apropriate?

什么标准异常类型最合适?

8 个解决方案

#1


26  

Well, Dictionary<,>.Add() throws ArgumentException if such key already exists, so I guess this could be a precedent.

好吧,Dictionary <,>。如果这样的键已经存在,Add()会抛出ArgumentException,所以我想这可能是一个先例。

#2


5  

ArgumentException would probably be the best. This is the exception thrown when an argument is invalid.

ArgumentException可能是最好的。这是参数无效时抛出的异常。

#3


3  

Linq uses two more exceptions DuplicateNameException and DuplicateKeyException you can use these if you are using system.data assembly.

如果使用system.data程序集,Linq使用另外两个异常DuplicateNameException和DuplicateKeyException可以使用它们。

#4


2  

I would use InvalidOperationException:

我会使用InvalidOperationException:

The exception that is thrown when a method call is invalid for the object's current state.

方法调用对于对象的当前状态无效时引发的异常。

Since the validity of the argument's value is contingent upon the state of the object (that is whether or not collection.Contains(item) is true) I think this is the best exception to use.

由于参数值的有效性取决于对象的状态(即collection.Contains(item)是否为true),我认为这是最好的例外。

Make sure that you add a good message to the exception that makes it clear what the problem was to the caller.

确保向异常添加一条好消息,以便明确调用者遇到的问题。

#5


2  

ArgumentException would be the proper exception (Dictionary uses that exception as well)

ArgumentException将是正确的异常(Dictionary也使用该异常)

#6


1  

System.ArgumentException

#7


1  

I would throw an ArgumentException. That's what the generic System.Collections.Generic.SortedList<> does in its Add method.

我会抛出一个ArgumentException。这就是泛型System.Collections.Generic.SortedList <>在其Add方法中的作用。

From the .NET Framework 2.0 code:

从.NET Framework 2.0代码:

    public void Add(TKey key, TValue value)
    {
        if (key == null)
        {
            System.ThrowHelper.ThrowArgumentNullException(System.ExceptionArgument.key);
        }
        int num = Array.BinarySearch<TKey>(this.keys, 0, this._size, key, this.comparer);
        if (num >= 0)
        {
            System.ThrowHelper.ThrowArgumentException(System.ExceptionResource.Argument_AddingDuplicate);
        }
        this.Insert(~num, key, value);
    }

#8


0  

I'd say InvalidOperationException, because it is not valid to add an object that's already in the collection

我会说InvalidOperationException,因为添加已经在集合中的对象是无效的

#1


26  

Well, Dictionary<,>.Add() throws ArgumentException if such key already exists, so I guess this could be a precedent.

好吧,Dictionary <,>。如果这样的键已经存在,Add()会抛出ArgumentException,所以我想这可能是一个先例。

#2


5  

ArgumentException would probably be the best. This is the exception thrown when an argument is invalid.

ArgumentException可能是最好的。这是参数无效时抛出的异常。

#3


3  

Linq uses two more exceptions DuplicateNameException and DuplicateKeyException you can use these if you are using system.data assembly.

如果使用system.data程序集,Linq使用另外两个异常DuplicateNameException和DuplicateKeyException可以使用它们。

#4


2  

I would use InvalidOperationException:

我会使用InvalidOperationException:

The exception that is thrown when a method call is invalid for the object's current state.

方法调用对于对象的当前状态无效时引发的异常。

Since the validity of the argument's value is contingent upon the state of the object (that is whether or not collection.Contains(item) is true) I think this is the best exception to use.

由于参数值的有效性取决于对象的状态(即collection.Contains(item)是否为true),我认为这是最好的例外。

Make sure that you add a good message to the exception that makes it clear what the problem was to the caller.

确保向异常添加一条好消息,以便明确调用者遇到的问题。

#5


2  

ArgumentException would be the proper exception (Dictionary uses that exception as well)

ArgumentException将是正确的异常(Dictionary也使用该异常)

#6


1  

System.ArgumentException

#7


1  

I would throw an ArgumentException. That's what the generic System.Collections.Generic.SortedList<> does in its Add method.

我会抛出一个ArgumentException。这就是泛型System.Collections.Generic.SortedList <>在其Add方法中的作用。

From the .NET Framework 2.0 code:

从.NET Framework 2.0代码:

    public void Add(TKey key, TValue value)
    {
        if (key == null)
        {
            System.ThrowHelper.ThrowArgumentNullException(System.ExceptionArgument.key);
        }
        int num = Array.BinarySearch<TKey>(this.keys, 0, this._size, key, this.comparer);
        if (num >= 0)
        {
            System.ThrowHelper.ThrowArgumentException(System.ExceptionResource.Argument_AddingDuplicate);
        }
        this.Insert(~num, key, value);
    }

#8


0  

I'd say InvalidOperationException, because it is not valid to add an object that's already in the collection

我会说InvalidOperationException,因为添加已经在集合中的对象是无效的