Catch ArgumentOutOfRangeException

本文关键字:ArgumentOutOfRangeException Catch | 更新日期: 2023-09-27 18:37:22

我有一个由计时器每秒运行一次的函数。该函数的目的是通过 API 请求数据,然后使用结果更新列表和一些文本框。在大多数情况下,它像发条一样运行,但每隔几个小时我就会得到一个 ArgumentOutOfRangeException。

无论出于何种原因,要么一个 API 请求失败,要么列表更新速度不够快。无论哪种方式,该函数都会尝试使用空列表更新文本框和变量,因此 ArgumentOutOfRangeException。

现在,这些数据大多不会存储任何时间长度,如果不是因为弹出错误并停止所有内容,该函数无论如何都会在另一秒钟内再次运行。在我制作这个程序之前,我没有使用过 C#,所以我不确定如何最好地利用"catch"语句让程序忽略它并继续前进。实际上,如果将失败次数记录在整数变量中,以便程序可以确定是否确实出错,而不仅仅是短暂的故障,那就太好了。你会如何为此编写捕获语句?

        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
        {
        }

Catch ArgumentOutOfRangeException

 try
 {
    // ...
 } 
 catch(ArgumentOutOfRangeException ex)
 {
    LogException(ex);
 }
相关文章:
  • 没有找到相关文章