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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:46:55+00:00 2026-05-15T23:46:55+00:00

If I am using a try/catch/finally block where and how should I initialize variables?

  • 0

If I am using a try/catch/finally block where and how should I initialize variables? For example say I’m trying to use a FileStream . I want to catch any exceptions thrown while creating or using the stream. Then regardless of whether there were any problems or not I want to ensure any stream created is closed.

So I’d do something like this:

        System.IO.FileStream fs;
        try
        {
            fs = new System.IO.FileStream("C:\test.txt", System.IO.FileMode.Open);
            //do something with the file stream
        }
        catch (Exception exp)
        {
            //handle exceptions
        }
        finally
        {
            //ERROR: "unassigned local variable fs"
            if (fs != null)
            {
                fs.Close();
            }
        }

However this gives me an error in the finally block saying unassigned local variable fs. Yet, if I change the declaration of fs to System.IO.FileStream fs = null it works.

Why do I need to explicitly set fs to null? I’ve also tried declaring fs in the try block, but then I get the error The name fs does not exsist in the current context in finally block.

BTW: I know I could use a Using block, but the point of my question is to understand the correct usage of a try/catch/finally block.

  • 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-15T23:46:56+00:00Added an answer on May 15, 2026 at 11:46 pm

    See section 5.3 of the specification.

    http://msdn.microsoft.com/en-us/library/aa691172(VS.71).aspx

    At a given location in the executable
    code of a function member, a variable
    is said to be definitely assigned if
    the compiler can prove, by static flow
    analysis, that the variable has been
    automatically initialized or has been
    the target of at least one assignment.

    With your try/catch/finally, the assignment of the try block cannot be guaranteed when you attempt to access the object in the finally block. As you have seen, you can meet the requirement by assigning an initial value to the variable (null, in this case).

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

Sidebar

Related Questions

I'm trying to throw an exception (without using a try catch block) and my
I used to close an open datareader using the try/catch/finally block: Dim dr As
I am writing a stored procedure where i m using try catch block. Now
I was just wondering which would be cheaper, using a try catch block for
I am currently using two catch blocks after a try block. The first catches
I understand how try-catch works and how try-finally works, but I find myself using
Using a try-catch-finally construction to retrieve a database record, it seems that I need
Out of habit I've been using try/catch blocks in my application code for all
How can I avoid NullPointerExceptions ? I have tried using try-catch blocks but that
Anyone knows why my sql query doesn't work after using a try catch function?

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.