I think what you're asking is this:
When parsing a file line by line, sometimes the current line has malformed data which causes an exception to be thrown in your code. Perhaps you simply need to structure your code such that the try/catch only surrounds the parsing code, not the line-reading code. For instance:
using System;
using System.IO;
using System.Windows.Forms;
public void ParseFile(string filepath)
{
TextReader reader = null;
try
{
reader = new StreamReader(filepath);
string line = reader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
try
{
// parse line here; if line is malformed,
// general exception will be caught and ignored
// and we move on to the next line
}
catch (Exception)
{
// recommended to at least log the malformed
// line at this point
}
line = reader.ReadLine();
}
}
catch (Exception ex)
{
throw new Exception("Exception when trying to open or parse file", ex);
}
finally
{
if (reader != null)
{
reader.Close();
reader.Dispose();
}
reader = null;
}
}
The outer try/catch block is to handle closing the reader object properly if something happened when trying to open or read the file. The inner try/catch block is to swallow exceptions raised if the read data was malformed and couldn't be parsed correctly.
I agree with pretty much everyone else, though. Just swallowing the exception might not be good. At the very least, I recommend you log it somewhere.