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

  • SEARCH
  • Home
  • 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 6990885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:25:02+00:00 2026-05-27T19:25:02+00:00

As it is, I tried error-proofing my code and ended up making it look

  • 0

As it is, I tried error-proofing my code and ended up making it look quite messy.

I have a function set up to read a certain type of file. I want the function to return false if there was a problem, or true if everything worked. I’m having trouble figuring out how to structure everything.

I have an initial try-catch block that tries to open a file stream. After that though, I have certain other checks I make during the reading process such as file size and values at certain offsets. The way I set it up was with if else statements. Such as:

if(condition){

}
else{
    MessageBox.Show("There was an error");
    br.Dispose();
    fs.Dispose();
    return false;
}

…br being the binary reader and fs the filestream. There are many blocks like this, and it seems like bad practice to write the same thing so many times. The first thing that comes to mind is to wrap the entire thing in a try-catch statement and throw exceptions instead of using the if else blocks. I remember when reading about try-catch statements that it’s good to have them, but not to wrap everything with them. To be honest, I still don’t completely understand why it would be bad practice to wrap everything in try catch statements, as they only have an effect when there’s an error, in which case the program is going south anyway…

Also, do I have to close the binary reader and file stream, or will closing one close the other? Is there any way to use them without having to dispose of them?

  • 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-27T19:25:03+00:00Added an answer on May 27, 2026 at 7:25 pm

    How about making use of the using keyword? this wraps your use of an IDisposable in a try – finally block;

    bool success = true;
    
    using(var fs = new FileStream(fileName, FileMode.Create)))
    using(var br = new BinaryReader(fs))
    {
      // do something
      success = result;
    }
    
    return success;
    

    The nested using blocks will ensure that both the filestream and binary reader are always properly closed and disposed.

    You can read more about using in MSDN. It makes the use of IDisposable a little neater, removing the need for explicit excpetion handling.

    Regarding your statement:

    I remember when reading about try-catch statements that it’s good to
    have them, but not to wrap everything with them.

    I always use the simple rule that if I cannot handle and recover from an exception within a particular block of code, don’t try to catch it. Allow the exception to ‘bubble up’ the stack to a point where it makes more sense to catch it. With this approach you will find that you do not need to add many try-catch blocks, you will tend to use them at the point of integration with services (such as filesystem, network etc …) but your business logic is almost always free of exception handling mechanisms.

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

Sidebar

Related Questions

i have this code and it is giving me this error i tried really
I've tried to override error message when input incorrect data type in input field
Error I tried this page to run on local, it is running perfectly fine
I received a Too many open files error when tried to do fopen (C++,
I tried to show an error message using the jquery effect fadeTo and tried
Fatal error: Allowed memory size of 31457280 bytes exhausted (tried to allocate 9828 bytes).
I got this error when I tried to compile an application that includes the
I tried this: ALTER TABLE My.Table DROP MyField and got this error: -MyField is
I received the following error when I tried to run a C# WinForms application
I've tried the below script but I am getting an error: dim cn, rs

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.