Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 358133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:16:15+00:00 2026-05-12T12:16:15+00:00

The using(…) statement is syntactic sugar for try{} finally {}. But if I then

  • 0

The using(…) statement is syntactic sugar for try{} finally {}.

But if I then have a using statement like below:

using (FileStream fs = File.Open(path))
{


}

Now I want to catch the exceptions that opening this file could cause (and this is fairly high risk code in that it can fail due to the environment), but if I write try-catch inside would that not be repetition? When the code gets compiled to IL, I assume the repetition will get deleted when the code is JITted?

However, I would want to catch the exceptions opening a file can cause (so I should wrap the try-catch outside the using statement’s scope), and also the exceptions for whatever I do inside the using block so I should add the try-catch inside the block.

This seems like I am adding a lot of repetition to what the CLR probably does inside. Does the CLR add catch clauses?

My colleague argued that a using statement is messy (but this was because a single line was slightly long due to me hard coding them as I needed to change them very quickly and didn’t have access to other parts of the code base). Said colleague doesn’t use the using statement but is there ever any functional difference between the using statement and try-finally/try-catch-finally? I did see one case of this where WCF services have a not well known corner case about using finally and returning values (something about finally). The solution was to use a check block. Is there anything like this in C#?

On another note, are all types which implement IDisposale owners of unmanaged resources? A discussion with my friend pointed the answer to being no. (I have also read some threads in the using section of this forum, some very good knowledge there).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:16:15+00:00Added an answer on May 12, 2026 at 12:16 pm

    You can implement the pattern yourself if you need to handle some exceptions if you really want to. Personally, I still find it simpler (and more importantly, clearer) to just wrap the using in try/catch block.

    But if you do it yourself, make sure you get it right. The Using block also creates an anonymous scope block so that your variable becomes eligible for collection sooner. The .Dispose() method that’s called at the end of the Using block only cleans up unmanaged resources, and so any memory your object holds may hang around a little longer. It’s not likely a huge concern, but it is worth remembering just in case.

    So, to do a direct adaption of the pattern, your code needs to look more like this:

    {
        FileStream fs;
        try
        {
            fs = File.Open(path);
    
        }
        catch (FileNotFoundException e) { /* ... */ }
        catch (IOException e) { /* ... */ }
        catch (Exception e) {/* ... */}
        finally
        {
            if (fs != null) fs.Dispose();
        }
    }
    

    Personally, I’d like to see Using expanded to support Catch and Finally blocks. Since they’re already performing a transformation on the code, it doesn’t seem like this would add that much additional complexity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 176k
  • Answers 176k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The FileInfo/DirectoryInfo classes from .NET (I assume all of this… May 12, 2026 at 3:11 pm
  • Editorial Team
    Editorial Team added an answer You should be able to use skin code like this… May 12, 2026 at 3:11 pm
  • Editorial Team
    Editorial Team added an answer I haven't actually tested it, but you shouldn't be losing… May 12, 2026 at 3:11 pm

Related Questions

The using(...) statement is syntactic sugar for try{} finally {}. But if I then
Does the using catch the exception or throw it? i.e. using (StreamReader rdr =
Does the using statement really call the close method when used with a database
I'm using the 'using' declaration in C++ to add std::string and std::vector to the
Assume the following code: using (SqlConnection conn = new SqlConnection(connectionString)) { ... using (SqlCommand

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.