Unassigned error in Catch

T

tshad

I have a streamwriter that I set up outside of my try/catch area, but I am
getting an error that it is not assignedin the Catch area:

Use of unassigned local variable 'objStreamWriter'

The code is essentially:

*************************************************************************************************
private void Button1_Click(object sender, System.EventArgs e)
{
...

StreamWriter objStreamWriter;

try
{
...

objStreamWriter = new StreamWriter(Server.MapPath("Employee.txt"));

...

objStreamWriter.Close();
return;
}
Catch (Exception ex)
{
objStreamWriter.Close();
}
}
*************************************************************************************************

Do I need to do something else where I set up the objStreemWriter?

Thanks,

Tom
 
N

Nicholas Paldino [.NET/C# MVP]

tshad,

You need to assign a value to the variable upon declaration. This
should fix it:

StreamWriter objStreamWriter = null;

BTW, you should move the call to close into a finally statement, or use
a using statement.

Hope this helps.
 
T

tshad

Nicholas Paldino said:
tshad,

You need to assign a value to the variable upon declaration. This
should fix it:

StreamWriter objStreamWriter = null;

BTW, you should move the call to close into a finally statement, or use
a using statement.

That was it.

And you're right.

I will move it into the Finally section.

Thanks,

Tom
Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

tshad said:
I have a streamwriter that I set up outside of my try/catch area, but I am
getting an error that it is not assignedin the Catch area:

Use of unassigned local variable 'objStreamWriter'

The code is essentially:
****************************************************************************
*********************
private void Button1_Click(object sender, System.EventArgs e)
{
...

StreamWriter objStreamWriter;

try
{
...

objStreamWriter = new StreamWriter(Server.MapPath("Employee.txt"));

...

objStreamWriter.Close();
return;
}
Catch (Exception ex)
{
objStreamWriter.Close();
}
}
****************************************************************************
*********************
Do I need to do something else where I set up the objStreemWriter?

Thanks,

Tom
 
B

Bill Butler

Nicholas Paldino said:
tshad,

You need to assign a value to the variable upon declaration. This should fix it:

StreamWriter objStreamWriter = null;

BTW, you should move the call to close into a finally statement, or use a using statement.

If you do that you need to be careful
-------------- Example1 --------------------
StreamWriter sw = null;
try
{
sw = new StreamWriter(""); //causes ArgumentException
}
finally
{
sw.Close(); // causes NullReferenceException
}


If you move the call to Close() to the finally statement, you need to check that objStreamWriter was
actually created.
If an exception was thrown objStreamWriter will be null

-------------- Example2--------------------
StreamWriter sw = null;
try
{
sw = new StreamWriter(""); //causes ArgumentException
}
finally
{
if (sw != null) // prevent NullReferenceException
sw.Close();
}



A cleaner implementation is the following:
-------------- Example3--------------------

using(StreamWriter sw = new StreamWriter("")) //causes ArgumentException
{
Console.WriteLine("hello");
}


*NOTE* None of these implementations actually catch the ArgumentException.
You will need a higher level try/catch to handle that


Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top