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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:25:19+00:00 2026-05-23T21:25:19+00:00

So, I need to create a file using the StreamWriter class, write to it

  • 0

So, I need to create a file using the StreamWriter class, write to it and then close it, and I want to put all the potentially inexecutable operations in try blocks; and as the file creation and write operations will be in different try blocks, I can’t initialize the StreamWriter variable (create the file) in one try block and then use it in another. I guess I could declare the StreamWriter variable outside the try blocks and set it to null, then initialize it in a try block, but MSDN says “When inside a try block, only initialize variables that are declared therein; otherwise, an exception can occur before the execution of the block is completed”. What is the best practice for safely declaring, initializing, using a file stream variable (or any variable, for that matter) and then disposing of it? Maybe I should use something instead of StreamWriter? Also, why shouldn’t you initialize a variable in a try block if you hadn’t declared it there?
Thanks.

  • 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-23T21:25:20+00:00Added an answer on May 23, 2026 at 9:25 pm

    The StreamWriter’s lifetime should be managed by a using block.

    Your instinct to move the variable declaration outside of the two try blocks is correct if you want to access it from within both. The MSFT guidance is just trying to impress upon you that in that case if you have possible exceptions occurring after declaration but before the try blocks they won’t be caught, but initializing a variable to null will not throw an exception and if you don’t do anything other than that you are fine. (If you were really worried about it though, you could just create a try/catch that encapsulates the variable declaration and also the other try blocks) Personally I am not a fan of a lot of try/catch but that’s a sermon for another day…

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

Sidebar

Related Questions

i need to create a object Image from import javax.microedition.lcdui.Image; using a file .png
I'm trying to create file upload using iframe(ajax effect) but I need to know
I need to create a new XML file and write that to my server.
I need to create a jar file using eclipse IDE. But I am facing
I need to create a pdf file using Itext , here is the code
I need to create a text file (unless it already exists) and write a
I need to create/append a text file and write some data in that. When
I need to create application log file using Linq-To-Xml. I would like to lock
I need to create a diff file using standard UNIX diff command with python
I want to create a PDF file using zend_pdf. But this way seems not

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.