An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
23,278
I think error line is here:
return filterList[filterList.FindIndex(delegate(ProcessInformation proc)
{
return proc.name.Equals(applicationNameList[countApplicationList.IndexOf(max)], StringComparison.Ordinal);
})];
Because List<T>.FindIndex
can return -1
when you can't find index.
Instead you should test if the index is less than 0, which indicates there is an error, before you use it:
int result = filterList.FindIndex(delegate(ProcessInformation proc)
{
return proc.name.Equals(applicationNameList[countApplicationList.IndexOf(max)], StringComparison.Ordinal);
});
if(result < 0) throw new Exception("Cant't Find ProcessInformation");
return filterList[result];
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on July 25, 2022Comments
-
Admin almost 2 years
In the following code, I got following error.
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative and less than the size of the collection.
Here is code:
public ProcessInformation GetMaxRunTimeForApplicationsBetween(DateTime StartingTime, DateTime EndingTime) { //Filter Based on Timer List<ProcessInformation> filterList = new List<ProcessInformation>(); foreach (HarvestApp.ProcessInformation item in this.ProcessList) { if (item.started_at.CompareTo(StartingTime) >= 0 && item.ended_at.CompareTo(EndingTime) <= 0) { filterList.Add(item); } } //Count Max Occurrence of Application List<int> countApplicationList = new List<int>(); List<string> applicationNameList = new List<string>(); foreach (HarvestApp.ProcessInformation item in filterList) { if (applicationNameList.Contains(item.name)) { countApplicationList[applicationNameList.IndexOf(item.name)]++; } else { applicationNameList.Add(item.name); countApplicationList.Add(1); } } //if (countApplicationList.Count == 0) //{ // throw new InvalidOperationException("Empty list"); //} int max = int.MinValue; foreach (int item in countApplicationList) { if (item > max) { max = item; } } //Return corresponding ProcessInformation Class of applicationNameList return filterList[filterList.FindIndex(delegate (ProcessInformation proc) { return proc.name.Equals(applicationNameList[countApplicationList.IndexOf(max)], StringComparison.Ordinal); })]; }
-
Damien_The_Unbeliever about 11 yearsGuessing C# from the exception name and the code style, but if not, please re-tag with an appropriate language tag.
-
Mike Perrenoud about 11 yearsThis exception could occur in a number of places in this code. What line is it failing on? You know that much.
-
Admin about 11 yearsexception occurrs at return statement.
-
Andre about 11 yearsWhat about the stacktrace?
-
-
Admin about 11 yearsI tried this. But still gives me same exception. It shows problem at Return statement.
-
Admin about 11 yearsYes. Error in return statement.