C#try / catch问题

时间:2014-04-04 20:49:40

标签: c#

因此,编写一个程序来读取文本文件,然后将该数字加倍并写入另一个文本文件。 即使它在try / catch块中,似乎如果输入文件名与预先存在的文件名不匹配,我会得到一个loping错误,而不是正确捕获和处理错误。 这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ICA28
{
class Program
{
    static void Main(string[] args)
    {
        string slInput;
        Console.Write("Please enter the input name: ");
        string sOpen = Console.ReadLine();
        sOpen = sOpen + ".txt";
        Console.WriteLine();
        Console.Write("Please enter the output name: ");
        string sSave = Console.ReadLine();
        sSave = sSave + ".txt";
        StreamReader srRead;
        StreamWriter swWrite;
        bool bError = true;
        while (bError == true)
        {
            try
            {
                srRead = new StreamReader(sOpen);
                swWrite = new StreamWriter(sSave);
                while (bError == true)
                {
                    try
                    {
                        while ((slInput = srRead.ReadLine()) != null)
                        {
                            double dDoub = double.Parse(srRead.ReadLine());
                            dDoub = dDoub * 2;
                            swWrite.WriteLine(dDoub);
                        }
                        swWrite.Close();
                        srRead.Close();
                        bError = false;
                    }

                    catch (Exception e)
                    {
                        Console.WriteLine("Error! {0}", e.Message);
                        bError = true;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error! {0}", e.Message);
                bError = true;
            }
        }
    }
}

}

2 个答案:

答案 0 :(得分:1)

在catch块中将bError设置为false

答案 1 :(得分:0)

你在循环中读了两遍。当第一个读取被检查为null时,第二个被认为是理所当然的,但如果你只有一行(或输入流中的奇数行),肯定会破坏你的代码。

    string slInput;
    Console.Write("Please enter the input name: ");
    string sOpen = Console.ReadLine();
    sOpen = sOpen + ".txt";
    if(!File.Exists(sOpen))
    {
        Console.WriteLine("Input file doesn't exist");
        return;  // Exit or put some kind of retry to ask again the input file
    }
    Console.WriteLine();
    Console.Write("Please enter the output name: ");
    string sSave = Console.ReadLine();
    sSave = sSave + ".txt";

   try
   {
        using(StreamReader srRead = new StreamReader(sOpen))
        using(StreamWrite swWrite = new StreamWriter(sSave))
        {
            while ((slInput = srRead.ReadLine()) != null)
            {
                double dDoub = double.Parse(slInput);
                dDoub = dDoub * 2;
                swWrite.WriteLine(dDoub);
            }
        }
    }
    catch (Exception e)
    {
         Console.WriteLine("Error! {0}", e.Message);
    }

请注意,我已将您的两个流放在一个使用块中,因此,如果您收到异常,它们将被关闭并正确处理。

此外,只有一个外部try catch可以处理内部循环所引发的所有重复,包括打开文件时可能出现的错误。因此,除非你需要在第一行之后突破,否则你不需要任何复杂的状态逻辑来退出循环