简体   繁体   English

C# 尝试/捕获问题

[英]C# try/catch issue

So, writing a program to read a text file, then double the number and write to a different text file.因此,编写一个程序来读取文本文件,然后将数字加倍并写入不同的文本文件。 Even though it is in a try/catch block, it seems that if the input file name does not match a preexisting file name, I get a loping error, instead of the error being caught and handled correctly.即使它在 try/catch 块中,似乎如果输入文件名与预先存在的文件名不匹配,我会收到一个 loping 错误,而不是错误被正确捕获和处理。 Here is my code:这是我的代码:

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

} }

在 catch 块中将 bError 设置为false

You are reading two times inside the loop.您正在循环内阅读两次。 While the first read is checked for null, the second one is taken for granted but this surely will break your code if you have only one line (or an odd number of lines in the input stream)虽然检查第一次读取是否为空,但第二次被认为是理所当然的,但是如果您只有一行(或输入流中的奇数行),这肯定会破坏您的代码

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

Notice that I have put your two streams inside a using block, so, if you get an exception they are closed and disposed correctly.请注意,我已将您的两个流放在 using 块中,因此,如果您遇到异常,它们将被关闭并正确处理。

Also, just one external try catch could handle all the excpetions reaised by the internal loop including possible errors when opening the files.此外,只需一个外部 try catch 就可以处理内部循环引发的所有异常,包括打开文件时可能出现的错误。 So, unless you need to break out after the first line, you don't need any complex state logic to exit from your loop因此,除非您需要在第一行之后中断,否则您不需要任何复杂的状态逻辑来退出循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM