ConcurrentSet<T>
을 구현하는 데 ConcurrentDictionary<TKey, TValue>
을 사용합니다.래핑 된 객체의 예외를 다시 throw
public class ConcurrentSet<T> : ISet<T>
{
private readonly ConcurrentDictionary<T, byte> collection;
}
ConcurrentDictionary<TKey, TValue>
은 null 키가있는 쌍을 포함 할 수 없습니다.
// summary, param, returns...
/// <exception cref="ArgumentNullException">
/// <paramref name="item" /> is null.
/// </exception>
public bool Add(T item)
{
// This throws an argument null exception if item is null.
return this.collection.TryAdd(item, 0);
}
그래서, 내가해야합니까
if (item == null)
throw new ArgumentNullException("item", "Item must not be null.");
return this.collection.TryAdd(item, 0);
을해야 또는한다 I,
try
{
return this.collection.TryAdd(item, 0);
}
catch (ArgumentNullException)
{
throw new ArgumentNullException("item", "Item must not be null.");
}
을 또는한다 I,
try
{
return this.collection.TryAdd(item, 0);
}
catch (ArgumentNullException x)
{
// I know, but I don't want to preserve the stack trace
// back to the underlying dictionary, anyway.
throw x;
}
을 또는
try
{
return this.collection.TryAdd(item, 0);
}
catch (ArgumentNullException)
{
// The thrown exception will have "key", instead of
// "item" as the parameter's name, in this instance.
throw;
}
이 작업을 수행하는 올바른 방법은 무엇입니까? 당신의 예에서
예외를 다시 던지면서 SO 응답을 확인하십시오. http://stackoverflow.com/questions/881473/why-catch-and-rethrow-exception-in-c –
요점은 무엇인지에 대한 정보를 추가해주세요. ArgumentNullException'을'ArgumentNullException'으로 래핑 할 때? –
@IlyaIvanov : 근본적인 문제는 'ConcurrentSet.Add'가 null 값으로 호출 되었기 때문에 스택 추적이 표시되어야한다는 것입니다. 게다가 throw 된 예외는 null 인수의 이름이 "key"가 아니라 "item"임을 나타냅니다. – supercat