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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:02:13+00:00 2026-05-18T02:02:13+00:00

think about this: StreamReader reader = null; try { reader = new StreamReader(_fileName); }

  • 0

think about this:

StreamReader reader = null;

try
{
    reader = new StreamReader(_fileName);
}
catch
{
    //show error that file not found
    reader.Dispose();
    return false;
}

try
{
    //code to read file
}
catch
{
   //show error badly formed file
}
finally
{
    reader.Dispose();
}
//return 

the code above does not work when file can’t be opened because it calls Dispose for null which results in exception.

i don’t want to use using because i want to separate problems with opening the file and reading it. This could be achieved with million different catches but i don’t want to go that way. Plus if using is same as try-finally would “hidden Dispose” throw an unwanted exception anyway? which would be the best way when all i need is to catch an exception opening it and an exception reading it?

Thanks & BR – Matti

  • 1 1 Answer
  • 1 View
  • 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-18T02:02:14+00:00Added an answer on May 18, 2026 at 2:02 am

    It is better to use the using statement:

    using(StreamReader reader = new StreamReader(_fileName))
    {
    }
    

    The compiler will create the correct dispose semantics for you.

    And you can still use try with this, not worrying about disposing it yourself:

    try
    {
        using(StreamReader reader = new StreamReader(_fileName))
        {
             try
             {
                //code to read the file
             }
             catch
             {
                //show error badly formed file
             }
        }
    }
    catch
    {
        // show error that file not found
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What do you think about this build tool ? I'm thinking of migrating from
What do you guys think about this for a generic singleton? using System; using
Do you think that writing about software (i.e. having a blog) and speaking on
I get this error with reCaptcha: 'Input error: response: Required field must not be
This isn't about a side-by-side technical comparison, rather about how to think in jQuery
When you think about it, doesn't the REST paradigm of being resource-oriented boil down
I need to think about performance limitations of 100 mbps ethernet (including scenarios with
I'd like know what people think about using RAISERROR in stored procedures to pass
I needed to re-install my computer but I didn't think about exporting the data
Are there any good online resources for how to create, maintain and think about

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.