1

From what I am reading here if you instantiate something inside a using statement than it should catch any exceptions and skip the code block. I am still seeing unhandled exceptions happen for file not found with this:

        using (TextReader sv = File.OpenText(@"sv\.sv"))
        {
            char[] k = { ':' };
            lastWsp = sv.ReadLine().Split(k)[1];
        }

Am I misunderstanding the MSDN article or do I need to set a switch in the compiler?

6
  • Could you please copy over the section which you think is saying that? Commented Oct 7, 2014 at 15:33
  • 2
    From the docs: "The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object." Using will not handle any exceptions that occur when instantiating the object. Commented Oct 7, 2014 at 15:34
  • @ChrisDunaway try throwing an exception from the method and call it from using. Commented Oct 7, 2014 at 15:35
  • 1
    Using is not handling any exceptions. It only calls Dispose method on IDisposable object when there is an unhandled exception. Commented Oct 7, 2014 at 15:36
  • 2
    @Selman22 - I'm not sure what you trying to say. If an exception occurs during the instantiation of the TextReader (in this case), there will be nothing to Dispose. The using statement doesn't handle exceptions. If an exception occurs inside the body of the using, then the TextReader is guaranteed to be Disposed, but the exception will still not be handled. Commented Oct 7, 2014 at 15:38

3 Answers 3

5

using translates into try-finally block, it doesn't have catch, so you are misunderstanding the article. It will not catch any exception, It will only make sure to dispose the object in case of an exception.

In your case since it a FileNotFound exception, your object will not be initialized.

Your code would translate into something like:

{
    TextReader sv = null;
    try
    {
        sv = File.OpenText(@"sv\.sv");
        char[] k = { ':' };
        lastWsp = sv.ReadLine().Split(k)[1];
    }
    finally
    {

        if(sv != null)
            sv.Dispose();
    }
}

In the above code in case of exception, it will try to dispose your object sv. But the exception will remain unhandled .

Since in your code, the exception is FileNotFound your object sv will remain null (uninitialized) and hence there will be no reason to call Dispose. But imagine if you have valid file path, and you get an exception at sv.ReadLine().Split(k)[1]; then it will dispose your TextReader sv, and that exception will propagate up in the hierarchy because there is no catch block.

Sign up to request clarification or add additional context in comments.

Comments

5

Yes, you are misunderstanding the article.

using declares that the resource will be closed with Dispose regardless of any exceptions thrown within the block. It will not catch the exceptions for you.

Comments

2

MSDN article is saying that even if exception is thrown from within the using block you have a guarantee that .Dispose() will be called on, in your case, object sv. Thats all it says and in addition to that, you are given the instructions on how to achieve that using try and finally:

You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block

So it seems you misinterpreted the article.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.