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 752033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:44:05+00:00 2026-05-14T14:44:05+00:00

Having a mental block today, need a hand verifying my logic isn’t fubar’ed. Traditionally

  • 0

Having a mental block today, need a hand verifying my logic isn’t fubar’ed.

Traditionally I would do file i/o similar to this:

FileStream fs = null; // So it's visible in the finally block
try
{
   fs = File.Open("Foo.txt", FileMode.Open);

   /// Do Stuff
}
catch(IOException)
{
   /// Handle Stuff
}
finally
{
   if (fs != null)
      fs.Close();
}

However, this isn’t very elegant.

Ideally I’d like to use the using block to dispose of the filestream when I’m done, however I am unsure about the synergy between using and try/catch.

This is how i’d like to implement the above:

try
{
   using(FileStream fs = File.Open("Foo.txt", FileMode.Open))
   {
      /// Do Stuff
   }
}
catch(Exception)
{
   /// Handle Stuff
}

However, I’m worried that a premature exit (via thrown exception) from within the using block may not allow the using block to complete execution and clean up it’s object. Am I just paranoid, or will this actually work the way I intend it to?

  • 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-14T14:44:05+00:00Added an answer on May 14, 2026 at 2:44 pm

    You’re just being paranoid and it will work the way you intend it to 🙂

    A using statement is equivalent to a try/finally block, whether it’s inside a try/catch or not.

    So your code is similar to:

    try
    {
       FileStream fs = null;
       try
       {
           fs = File.Open("Foo.txt", FileMode.Open);
           // Do stuff
       }
       finally
       {
           if (fs != null)
           {
               fs.Dispose();
           }
       }
    }
    catch(Exception)
    {
       /// Handle Stuff
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I'm having a bit of a mental block today/this week, so I'm probably
Having a mental block.... I know this is so easy.... but nothing is going
Having a mental block with going around this query. I have the following tables:
Can’t be hard, but I’m having a mental block.
I'm having a mental block for the words describing data flow in a communications
Im having a bit of a mental block on this one. I got booking
Having a mental block due to caffeine depravation... If I add two drop-downs to
I am having a mental block and I know I should know this but
For some reason, I am having a mental block trying to figure out how
I know this should be painfully simple, but I'm having a mental block. I

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.