Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eating Exceptions

I m parsing a file that has MalFormed data from time to time.

and it s throwing an exception,

i d like to recover from the exception and ignore the bad formatted data.

What s the best way to do this?

try{
// parse file
}catch(Exception){
//eat it.
}

*EDIT:*I think, my question wasnt understood well. i d like to recover from the exception, any exception for that matter, i dont want my program to stop. but to continue.

like image 727
DarthVader Avatar asked Oct 19 '10 21:10

DarthVader


People also ask

What does it mean to eat an exception?

(Learn how and when to remove this template message) In computer programming, error hiding (or error swallowing) is the practice of catching an error or exception, and then continuing without logging, processing, or reporting the error to other parts of the software.

How do you handle exceptions?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.

What is a silent exception?

You can also define "silent" exceptions that do not, by default, cause the application to show the error message. Silent exceptions are useful when you don't intend to report an exception to the user, but you want to abort an operation.

Are exceptions an anti-pattern?

Generally, the use of exceptions for control flow is an anti-pattern, with many notable situation - and language-specific (see for example Python) cough exceptions cough. As a quick summary for why, generally, it's an anti-pattern: Exceptions are, in essence, sophisticated GOTO statements.


1 Answers

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.

like image 190
Thorin Avatar answered Oct 01 '22 12:10

Thorin