C# 尝试/捕获问题

本文关键字:问题 尝试 | 更新日期: 2023-09-27 17:56:37

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

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

}

C# 尝试/捕获问题

将 bError 设置为在 catch 块中false

你在循环内读了两次。虽然第一次读取被检查为 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);
    }

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

此外,只需一个外部尝试捕获即可处理内部循环处理的所有操作,包括打开文件时可能出现的错误。因此,除非你需要在第一行之后中断,否则你不需要任何复杂的状态逻辑来退出循环。