{"record":{"id":"8c9b3130278a791d","repo":"TheAlgorithms/C-Sharp","slug":"exception-of-type-utilities-exceptions-itemnotfoundexception","errorCode":null,"errorMessage":"Exception of type 'Utilities.Exceptions.ItemNotFoundException' was thrown.","messagePattern":"Exception of type 'Utilities\\.Exceptions\\.ItemNotFoundException' was thrown\\.","errorType":"exception","errorClass":"ItemNotFoundException","httpStatus":null,"severity":"error","filePath":"Algorithms/Search/FastSearcher.cs","lineNumber":27,"sourceCode":"///     average case: O(log log n),\n///     best case: O(1).\n///     Note: This algorithm is recursive and the array has to be sorted beforehand.\n/// </summary>\npublic class FastSearcher\n{\n    /// <summary>\n    ///     Finds index of first item in array that satisfies specified term\n    ///     throws ItemNotFoundException if the item couldn't be found.\n    /// </summary>\n    /// <param name=\"array\">Span of sorted numbers which will be used to find the item.</param>\n    /// <param name=\"item\">Term to check against.</param>\n    /// <returns>Index of first item that satisfies term.</returns>\n    /// <exception cref=\"ItemNotFoundException\"> Gets thrown when the given item couldn't be found in the array.</exception>\n    public int FindIndex(Span<int> array, int item)\n    {\n        if (array.Length == 0)\n        {\n            throw new ItemNotFoundException();\n        }\n\n        if (item < array[0] || item > array[^1])\n        {\n            throw new ItemNotFoundException();\n        }\n\n        if (array[0] == array[^1])\n        {\n            return item == array[0] ? 0 : throw new ItemNotFoundException();\n        }\n\n        var (left, right) = ComputeIndices(array, item);\n        var (from, to) = SelectSegment(array, left, right, item);\n\n        return from + FindIndex(array.Slice(from, to - from + 1), item);\n    }\n","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/Algorithms/Search/FastSearcher.cs#L9-L45","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nint idx = searcher.FindIndex(span, key);\n// after\nif (span.Length == 0 || key < span[0] || key > span[^1]) return -1;\nint idx = searcher.FindIndex(span, key);","handlingStrategy":"validation","validationCode":"bool searchable = array.Length > 0 && item >= array[0] && item <= array[^1];","typeGuard":"static bool CanContain(Span<int> array, int item) => array.Length > 0 && item >= array[0] && item <= array[^1];","tryCatchPattern":"try { int idx = searcher.FindIndex(array, item); }\ncatch (ItemNotFoundException) { return -1; } // item absent","preventionTips":["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"],"tags":["search","csharp","not-found"],"backgroundTag":"record-not-found","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}