Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How efficiently manage a Stream with try / catch / finally C#

I recently discussed with a coworker who told me that I was managing incorrently a stream into a try / catch / block. So I wanna know what would be a good approach for you.

try
{
    StreamReader sr = new StreamReader("TestFile.txt");
    //After that, here an operation of about 30 seconds to fulfill;
}
catch (IOException ioex)
{
    throw new IOException("An error occurred while processing the file.", ioex);
}
catch (Exception ex)
{
    throw new Exception("An generic error ocurred.");
}
finally
{
    if(sr != null){
        stream.Close();
        stream = null;
    }
}

He stated that having 2 Exception are unnecessary, even using the IOException. We can use only Exception. But the only thing that I want is to recognize where exactly the exception has been produced, because after opening the file, an operation of about 30 seconds will be performed.

So what would you think? We saw this MS example (http://msdn.microsoft.com/fr-Fr/library/system.io.streamreader.aspx) which it's simplier but in terms of performance or clean code, you find something strange?

Your opinions please!

-EDIT-----------------

Ok, I see the point but we were discussing about the Catch IOException and just using the Exception. In my opinion, like as in the example above you can know where the error ocurred; at the moment of managing the file or in the process after opening the file. That's my first question. And now, what do you think about this change below.

try
{
    using(StreamReader sr = new StreamReader("TestFile.txt"))
    {
        //After that, here an operation of about 30 seconds to fulfill;
    }
}
catch (Exception ex)
{
    throw new Exception("An generic error ocurred.");
}
finally
{
    if(sr != null){
        stream.Close();
        stream = null;
    }
}

-------------------EDIT 2------------------------

Finally, I hope this would be my final solution. Thank you so much for your answers. So using is faster, efficient and just one exception is necessary.

try
{
    using (StreamReader stream = sr = new StreamReader("TestFile.txt"))
    {
        //Operation
    }
}
catch (Exception e)
{
    throw new Exception(String.Format("An error ocurred while executing the data import: {0}", e.Message), e);
}

Any other comment would be appreciated!

like image 630
Maximus Decimus Avatar asked Oct 31 '13 02:10

Maximus Decimus


People also ask

What is the point of try-catch finally?

The try statement defines the code block to run (to try). The catch statement defines a code block to handle any error. The finally statement defines a code block to run regardless of the result. The throw statement defines a custom error.

Does try-catch reduce performance?

In general, wrapping your Java code with try/catch blocks doesn't have a significant performance impact on your applications. Only when exceptions actually occur is there a negative performance impact, which is due to the lookup the JVM must perform to locate the proper handler for the exception.

Do try-catch blocks hurt performance C#?

Still, there are some situations where the existence of a try/catch block may prevent optimizations which--but for the try/catch--would have allowed code to run faster. Save this answer. Show activity on this post. Yes, try/catch will "hurt" performance (everything is relative).

Can we use try-catch and finally together?

The try block cannot be present without either catch clause or finally clause. Any code cannot be present in between the try, catch, finally blocks.


1 Answers

you can use using block as below, and it will dispose the stream even on exception occurred

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
   // do something with sr
}

Catch Exception if you're going to do something about. If you can't fix the problem, there's no point in catching it.

if you can't resolve the exception, it's best to just let the exception bubble up the exception and catch it there.

try
{
    using(StreamReader sr = new StreamReader("TestFile.txt"))
    {
       // your code 
    }
}
catch (IOException ioex)
{
    // do something to fix the problem 
    // log the exception 
}
like image 134
Damith Avatar answered Sep 28 '22 17:09

Damith