TheAlgorithms/C-Sharp · error · ItemNotFoundException
Exception of type…
Error message
Exception of type 'Utilities.Exceptions.ItemNotFoundException' was thrown.
What it means
FastSearcher.FindIndex throws ItemNotFoundException when the item cannot be located: either the array is empty or the item lies outside the sorted array's [first, last] range, so binary search cannot contain it. It is the library's typed 'not found' signal.
Solutions
- Check array.Length > 0 and that item is within [array[0], array[^1]] before calling.
- Catch ItemNotFoundException and treat it as 'absent' rather than an exceptional case.
- If data may be unsorted, sort it first or use a linear search.
Example fix
// before int idx = searcher.FindIndex(span, key); // after if (span.Length == 0 || key < span[0] || key > span[^1]) return -1; int idx = searcher.FindIndex(span, key);
Defensive patterns
Strategy: validation
Validate before calling
bool searchable = array.Length > 0 && item >= array[0] && item <= array[^1];
Type guard
static bool CanContain(Span<int> array, int item) => array.Length > 0 && item >= array[0] && item <= array[^1];
Try / catch
try { int idx = searcher.FindIndex(array, item); }
catch (ItemNotFoundException) { return -1; } // item absent Prevention
- Range-check the item against first/last elements before binary search
- Ensure input arrays are sorted before using FastSearcher
- Handle 'not found' as a normal outcome, not only via exceptions
When it happens
Trigger: Calling FindIndex on an empty Span<int>, or searching for a value smaller than array[0] or larger than array[^1] in a sorted span.
Common situations: Searching for a key absent from a lookup table; passing unsorted or partially sorted data; stale cached min/max assumptions after data changed; searching empty results from an upstream query.
Understand the failure class
Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.
Related errors
- Exception of type…
- is not a positive integer
- is not invertible in Z/ Z.
- Adjacency matrix must be square!
- Adjacency matrix must be symmetric!
AI-assisted analysis of TheAlgorithms/C-Sharp@96e2905cab (2026-09-13).
Data as JSON: /api/errors/8c9b3130278a791d.
Report an issue: GitHub.
Appendix: source
Thrown at Algorithms/Search/FastSearcher.cs:27
/// average case: O(log log n),
/// best case: O(1).
/// Note: This algorithm is recursive and the array has to be sorted beforehand.
/// </summary>
public class FastSearcher
{
/// <summary>
/// Finds index of first item in array that satisfies specified term
/// throws ItemNotFoundException if the item couldn't be found.
/// </summary>
/// <param name="array">Span of sorted numbers which will be used to find the item.</param>
/// <param name="item">Term to check against.</param>
/// <returns>Index of first item that satisfies term.</returns>
/// <exception cref="ItemNotFoundException"> Gets thrown when the given item couldn't be found in the array.</exception>
public int FindIndex(Span<int> array, int item)
{
if (array.Length == 0)
{
throw new ItemNotFoundException();
}
if (item < array[0] || item > array[^1])
{
throw new ItemNotFoundException();
}
if (array[0] == array[^1])
{
return item == array[0] ? 0 : throw new ItemNotFoundException();
}
var (left, right) = ComputeIndices(array, item);
var (from, to) = SelectSegment(array, left, right, item);
return from + FindIndex(array.Slice(from, to - from + 1), item);
}
View on GitHub (pinned to 96e2905cab)