如何逐行读取文本文件中的数据时,如何捕获并忽略或处理异常

时间:2011-08-29 17:46:50

标签: c# exception-handling

我正在从文本文件中逐行读取文件并进行一些处理。问题是如果在某些行发生某些错误。然后生成一个异常,我想要的是我想忽略该错误并移动到下一行来读取。 但如果生成异常,那么我就无法继续读取输入行。请帮忙。

10 个答案:

答案 0 :(得分:4)

如果我正在假设您正确要求的内容,请参阅以下代码的基本概要:

using (StreamReader reader = File.OpenText("Path\to\your\file"))
{
    string line = null;
    while ((line = reader.ReadLine()) != null)
    {
        try
        {
            ProcessLine(line);
        }
        catch { /* Ignore exceptions */ }
    }
}

盲目捕获所有异常通常不是一个好主意,所以如果可以,你应该将catch块捕获的异常过滤为更具体的异常。

答案 1 :(得分:2)

答案 2 :(得分:2)

如果您真的想“忽略”异常,可以执行以下操作:

try
{
    foo(); // Something that may throw an exception
}
catch
{
}

有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/0yd65esw(v=vs.80).aspx

但通常,异常意味着发生了一些不好的事情,你可能想以某种方式处理它。

答案 3 :(得分:1)

try
{
//put the statement throwing the exception here
}
catch
{
//will eat the exception
}
//execution will continue here

答案 4 :(得分:1)

很难理解你想要达到的目标,但你可能会要求这样的事情:

while(condition)
{
    try {
        //process file line here
    }
    catch (Exception ex) {
        LogException(ex);
    }
}

顺便说一句,在我看来,这不是一个好的设计决定。如果可以,请避免使用它。

答案 5 :(得分:0)

使用try catch和记录错误。你的代码看起来像这样:

try
{
   //read lines here
}
catch(Exception ex)
{
   //log the exception but don't throw anything.
}

你可能很想在捕获中做任何事情,但你可能会后悔。

尝试捕捉文章:

http://www.homeandlearn.co.uk/csharp/csharp_s5p6.html

答案 6 :(得分:0)

您只需将处理代码包装在try / catch块中即可。

 try
 {
     DoSomeProcessing(lineThatIreadFromFile);
 }
 catch
 {
    // Log or Ignore error here
 }

但请注意,通常情况下,吞咽异常绝不是一个好主意。您应该使程序失败(如果不可恢复),或者可能将其记录在某处,以便您可以修复程序失败的原因。

答案 7 :(得分:0)

这不是一个好方法。您应该主动并捕获可以从中恢复的特定异常。抓住它们靠近它们被抛出的地方。让其余的人冒泡并终止这个过程。通过吞下所有异常,您将获得稳健的幻觉,而实际上您的代码可能充满了错误。没有“快速和肮脏”的异常处理方法。请参阅此answer

  

通过捕获非特定异常避免处理错误,例如   应用程序中的System.Exception,System.SystemException等   码。有些情况下处理应用程序中的错误   可以接受,但这种情况很少见。

     

应用程序不应处理可能导致异常的异常   意外或可利用的状态。如果你无法预测所有可能的   异常的原因并确保恶意代码无法利用   由此产生的应用程序状态,您应该允许应用程序   终止而不是处理异常。

答案 8 :(得分:0)

根据您提供的信息非常有限,您可以做两件事:

  • 用一个空的catch块包含违规行。等待下一个维护者给你做坏事。

  • 了解异常发生的原因并修改代码,以便下一位维护者理解为什么忽略某种情况是安全的

答案 9 :(得分:0)

你需要:

using System.IO;

让这个工作。

您可以尝试:

try
{
    string path = ""; // You must add the path here. Else it won't work.
    string[] lines = File.ReadAllLines(path);
    foreach(string line in lines)
    {
        Console.WriteLine(line);
    }
} catch (Exception ex, IOException ioex) {
    // It's optional. You can remove "Exception ex, IOException ioex" if you want. You can delete the code below too.
    Console.WriteLine(ex.ToString());
    Console.WriteLine();
    Console.WriteLine(ioex.ToString());
} finally
{
    // in this "finally" section, you can place anything else. "finally" section isn't important, just shows that method has no exceptions.
    // you can add something else like: Console.WriteLine("Code has no exceptions. Great!");
}

适用于高级记事本。

编辑:如果你不喜欢以前的解决方案,这个可以帮到你。

string path = ""; // Again, path.
string[] lines = File.ReadAllLines(path);
foreach(string line in lines)
{
    try
    {
        Console.WriteLine(line);
    } catch(Exception ex, IOException ioex)
    { /* exception */ }
}

-----或-----

string path = Console.ReadLine();
int turns = 0;
int maxturns = (File.ReadAllLines(path)).Count();
while (turns < maxturns)
{
    try
    {
        Console.WriteLine(File.ReadLines(path).Skip(turns).Take(1).First());
    } catch (Exception ex, IOException ioex) { /* exception */ }
    turns++;
}