Try/Catch Array Scope Problem

  • Thread starter Thread starter Matt Harvey
  • Start date Start date
M

Matt Harvey

The solution to this is most undoubtedly obvious, but I can't seem to
figure it out...your help is appreciated.

Let's say I have a block of code that looks like this:

string[] array1;
int i =0;

try
{
array1= new string[10];

while (i <10)
{
array1 = i;
i++;
}
}

catch
{
bool result = VerifyArray(array1);
}

When compiling, I get use of unassigned local variable on the catch
statement for array1.

As you can see, however, my array gets assigned inside of the try block.
If I try to initialize array1 to null when I declare it, assigning
values to it errors out.

Any ideas?
 
Matt Harvey said:
The solution to this is most undoubtedly obvious, but I can't seem to
figure it out...your help is appreciated

When compiling, I get use of unassigned local variable on the catch
statement for array1.

As you can see, however, my array gets assigned inside of the try block.

Assuming it gets that far. An exception could be thrown asynchronously
before that assignment happens.
If I try to initialize array1 to null when I declare it, assigning
values to it errors out.

You should initialize it to null before the try, then assign it as you
are doing within the try - or assign it to new string[10] before the
try to start with.

What *exactly* do you mean by "errors out"? Where does the exception
occur and what exception is it?
 
Matt,

The compiler cannot verify that array1 is necessarily assigned to
before the catch block executes. At first glance this may seem odd
because, as you mention, it is being assigned to at the beginning of
the try block. But, what happens if the runtime throws an
OutOfMemoryException or something during the assignment. The catch
block will execute and array1 will still be null.

Brian
 
Jon said:
Matt Harvey said:
The solution to this is most undoubtedly obvious, but I can't seem to
figure it out...your help is appreciated


When compiling, I get use of unassigned local variable on the catch
statement for array1.

As you can see, however, my array gets assigned inside of the try block.


Assuming it gets that far. An exception could be thrown asynchronously
before that assignment happens.

If I try to initialize array1 to null when I declare it, assigning
values to it errors out.


You should initialize it to null before the try, then assign it as you
are doing within the try - or assign it to new string[10] before the
try to start with.

What *exactly* do you mean by "errors out"? Where does the exception
occur and what exception is it?
By "errors out" I mean it fails on assignment if I initialize it to
null. I can't assign it to a new string[10] as the number of elements
can change each time this block is executed. I just threw 10 in there
for simplicity.
 
Matt Harvey said:
By "errors out" I mean it fails on assignment if I initialize it to
null. I can't assign it to a new string[10] as the number of elements
can change each time this block is executed. I just threw 10 in there
for simplicity.

You'll need to assign a real array to it at some point - when were you
planning on doing that? You can do new string[x] where x is some
variable.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 

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

Back
Top