Reputation: 13
I have a function that is run once every second by a timer. The purpose of the function is to request data through an API and then update a list and some textboxes with the results. For the most part it runs like clockwork, but every couple of hours I get an ArgumentOutOfRangeException.
For whatever reason, either that one API request fails or the list doesn't update fast enough. Either way the function tries to update texboxes and variables using an empty list, hence the ArgumentOutOfRangeException.
Now this data is mostly not being stored for any length of time, and the function would just run again in another second anyway if not for the error popping up and stalling everything. I hadn't used C# before I made this program so I'm not sure how best to utilize the "catch" statement to just make the program ignore it and keep going. Actually it would be good if the number of failures was logged in an integer variable so that the program could determine if something was actually wrong instead of just a momentary glitch. How would you write the catch statement for this?
try
{
GetQuoteResponse resp = GetQuoteResponse.GetQuote(questradeToken, quotesSymbolIds);
List<Level1DataItem> quotes = resp.Quotes;
QuoteStream_Data.Clear();
for (int i = 0; i < quotes.Count; ++i)
{
Level1DataItem quote = quotes[i];
QuoteStream_Data.Add(new QuoteStream_Entry { lastTradePrice = Convert.ToDecimal(quote.m_lastTradePrice)});
}
XIVPriceBox.Invoke(new Action(() => XIVPriceBox.Text = QuoteStream_Data[0].lastTradePrice.ToString()));
HVIPriceBox.Invoke(new Action(() => HVIPriceBox.Text = QuoteStream_Data[1].lastTradePrice.ToString()));
TVIXPriceBox.Invoke(new Action(() => TVIXPriceBox.Text = QuoteStream_Data[2].lastTradePrice.ToString()));
XIVCurrentPrice = QuoteStream_Data[0].lastTradePrice;
TVIXCurrentPrice = QuoteStream_Data[2].lastTradePrice;
}
catch
{
}
Upvotes: 0
Views: 2464
Reputation: 99987
try
{
// ...
}
catch(ArgumentOutOfRangeException ex)
{
LogException(ex);
}
Upvotes: 1