Should I throw my own ArgumentOutOfRangeException or let one bubble up from below?
Posted
by Neil N
on Stack Overflow
See other posts from Stack Overflow
or by Neil N
Published on 2010-03-26T20:14:00Z
Indexed on
2010/03/26
20:23 UTC
Read the original article
Hit count: 373
I have a class that wraps List<>
I have GetValue by index method:
public RenderedImageInfo GetValue(int index)
{
list[index].LastRetrieved = DateTime.Now;
return list[index];
}
If the user requests an index that is out of range, this will throw an ArgumentOutOfRangeException .
Should I just let this happen or check for it and throw my own? i.e.
public RenderedImageInfo GetValue(int index)
{
if (index >= list.Count)
{
throw new ArgumentOutOfRangeException("index");
}
list[index].LastRetrieved = DateTime.Now;
return list[index];
}
In the first scenario, the user would have an exception from the internal list, which breaks mt OOP goal of the user not needing to know about the underlying objects.
But in the second scenario, I feel as though I am adding redundant code.
Edit:
And now that I think of it, what about a 3rd scenario, where I catch the internal exception, modify it, and rethrow it?
© Stack Overflow or respective owner