【问题标题】:Why is try catch sometimes ignored?为什么 try catch 有时会被忽略?
【发布时间】:2021-07-05 15:00:34
【问题描述】:

一个例子是这段代码:

try
{
    string domain = o.SelectToken("response[" + i + "].domain").ToString();
    ...
}
catch(Exception)
{
    continue;
}

而不是仅仅在循环中继续(“继续”),vs 停止并指向string domain = o.SelectToken("response[" + i + "].domain").ToString(); 以获得System.IndexOutOfRangeException

这是为什么呢?

【问题讨论】:

  • 如果您的意思是在调试期间,这是一个允许 VS 在出现异常时停止异常的设置。
  • 你指的是什么循环?
  • @ScottHunter 聪明的程序员都知道,在 try catch 中继续只有在循环中才有意义。

标签: c# visual-studio exception visual-studio-debugging


【解决方案1】:

您可能在 Debug>Windows>Exception settings 中选择了“break on all exceptions”:

https://docs.microsoft.com/en-us/visualstudio/debugger/managing-exceptions-with-the-debugger?view=vs-2019

取消选择此选项将使 VS 继续。

【讨论】:

    【解决方案2】:

    你可以通过两种方式来做到这一点。

    1. 按照 MSDN 中的建议,是在您的 Visual Studio 中进行设置(我相信是 2019 年)

      Debug > Windows > Exception Settings:搜索index并取消勾选。

    2. 请在您的代码中添加异常以处理异常..

      catch(IndexOutOfRangeException e)
      {
      // handle it like logging it in file and continue
          continue;
      }
      catch(Exception)
      {
          continue;
      }
      

    【讨论】:

      猜你喜欢
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      • 2016-08-16
      相关资源
      最近更新 更多