【问题标题】:Why is a local variable assigned in one method, but unassigned in a near-identical method?为什么在一种方法中分配了局部变量,但在几乎相同的方法中未分配?
【发布时间】:2014-11-19 21:47:00
【问题描述】:

当我编译此代码时,我收到一个编译错误,指出 writerSaveArray 中未分配的局部变量。它不会以类似的方法LoadArray 抱怨reader。为什么会这样?他们不应该表现得一样吗?

    static void SaveArray(string fileName,  string[,] arr)
    {
        StreamWriter writer;
        try
        {
            writer = new StreamWriter(fileName);
        }
        catch
        {
            MessageBox.Show("Error, could not open " + fileName + " for saving");
        }
        try 
        {
            foreach (string entry in playerArray)
            {
                writer.WriteLine(entry);
            }
        }
        catch
        {
            MessageBox.Show("Couldn't save");
        }
        writer.Close();
    }

    static void LoadArray(string fileName, string[,] arr)
    {
        StreamReader reader;

        try
        {
            reader = new StreamReader( fileName );
        }
        catch
        {
            MessageBox.Show("Error when reading file" +fileName);
            return;
        }


        try
        {
            for(int i=0; i<=arr.GetUpperBound(0); ++i)
            {
                for (int j = 0; j<=arr.GetUpperBound(1); ++j)
                {
                    arr[i, j] = reader.ReadLine();
                }
            }

        }
        catch
        {
            MessageBox.Show("Could not read from file " +fileName);
        }
        reader.Close();
    }

【问题讨论】:

    标签: c# exception variable-assignment unassigned-variable


    【解决方案1】:

    如果new StreamWriter(fileName); 抛出异常,则s 保持未分配状态。

    尝试在s.WriteLine(entry); 中使用它会出错。

    正如 @DarrenYoung 所评论的,LoadArraycatch 返回,所以x.ReadLine() 中的x 保证会被初始化。

    【讨论】:

    • 好的,现在解释一下为什么x版本是好的。
    • @LeeTaylor 是因为你在第一个 catch 块中返回了 LoadArray 方法。
    • 我还能如何 s.WriteLine 条目?
    • @SzymonZmudzki 您可以从SaveArray 中的catch 块返回(与LoadArray 中的方式相同)。如果new StreamWriter 失败,继续执行有什么意义?
    【解决方案2】:

    LoadArray 中,捕获的异常会导致方法在读取器被使用之前返回。在SaveArray 中,它捕获了异常,然后继续其愉快的方式,即使作者从未完成分配。

    永远记住,捕获的异常会立即脱离正常的控制流,因此当前语句不会完成执行。

    【讨论】:

      【解决方案3】:

      请注意您是如何声明一个名为 writer 的 StreamWriter 对象,但您在进入 try/catch 块之前不会对其进行初始化。如果 try catch 失败会发生什么?

              StreamWriter writer;
              try
              {
                  writer = new StreamWriter(fileName);
              }
              catch
              {
                  MessageBox.Show("Error, could not open " + fileName + " for saving");
              }
              try 
              {
                  foreach (string entry in playerArray)
                  {
                      writer.WriteLine(entry);
                  }
              }
              catch
              {
                  MessageBox.Show("Couldn't save");
              }
              writer.Close();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-18
        • 2011-09-25
        • 2016-01-25
        • 1970-01-01
        • 2017-10-27
        • 2012-08-12
        • 2020-04-01
        相关资源
        最近更新 更多