我不明白爲什麼我今天收到System.IndexOutOfRangeException
。我已經收到它,當程序已經工作了8個小時,發生異常的地方被執行了數百萬次。這個地方是非常簡單的:System.IndexOutOfRangeException初始化一次,長度不變的集合,如何?
for (int i = 0; i < _goldDesiredOrdersBuy.Length; i++)
{
_goldDesiredOrdersBuy[i] = -1; // IndexOutOfRangeException! Strategy3.cs:line 666
}
我初始化_goldDesiredOrdersBuy只有當程序啓動,所以它保證,當出現異常它初始化一次:
private int[] _goldDesiredOrdersBuy = new int[MaxOrderbookDepth];
我有一個另一個地方,我接觸這個數組:
private int GetGoldVolumeBuy(int bidQuotesPos)
{
if (_goldDesiredOrdersBuy[bidQuotesPos] > -1)
{
return _goldDesiredOrdersBuy[bidQuotesPos];
}
int result = GetGoldVolumeBuyNotCached(bidQuotesPos);
_goldDesiredOrdersBuy[bidQuotesPos] = result;
return result;
}
而就是這樣。 _goldDesiredOrdersBuy
在應用程序啓動時初始化一次,保證初始化並且數組的長度不會在任何地方修改,所以我不明白我是如何收到IndexOutOfRangeException
的,有什麼想法?
Unhandled Exception: System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. --->
System.AggregateException: One or more errors occurred. --->
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at MyProj.Strategies.Strategy3.CalculateNewDesiredOrdersBuy() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 666
at MyProj.Strategies.Strategy3.RecalculateBuyOrders() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 567
at MyProj.Strategies.Strategy3.OnAllTablesUpdated() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 499
at MyProj.Strategies.Strategy3.AllTablesUpdated() in C:\Oleg\projects\MyProj\MyProj\Strategies\Strategy3.cs:line 413
at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.<ForWorker>b__c()
at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
at System.Threading.Tasks.Task.<>c__DisplayClass7.<ExecuteSelfReplicating>b__6(Object)
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
at System.Threading.Tasks.Task.Wait()
at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source, Action`1 body)
at MyProj.Market.FinishUpdatingTables() in C:\Oleg\projects\MyProj\MyProj\Market.cs:line 449
at Library.Exchange.Gate.DoGateIteration() in C:\Oleg\projects\MyProj\MyProj\Gate.cs:line 143
at Library.Exchange.Gate.<Connect>b__0() in C:\Oleg\projects\MyProj\MyProj\Gate.cs:line 98
at System.Threading.Tasks.Task.Execute()
--- End of inner exception stack trace ---
at System.Threading.Tasks.TaskExceptionHolder.Finalize()
請注意,該程序的不同部分發生異常。在非常微不足道的部分。 – javapowered
我們正面臨不可能的情況。因此,可用信息中的某些內容可能是錯誤的。我懷疑堆棧跟蹤的有效性(也給出了並行執行)。我仍然賭上一個明顯的地方。 – Steve
我明白了。那麼現在我包圍了發生異常的地方來捕獲它並打印更多的調試信息。 – javapowered