Connecting Tech Pros Worldwide Forums | Help | Site Map

Try/Catch Array Scope Problem

Matt Harvey
Guest
 
Posts: n/a
#1: Apr 25 '06
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;
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?

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#2: Apr 25 '06

re: Try/Catch Array Scope Problem


Matt Harvey <rifleman@gmail.com> wrote:[color=blue]
> The solution to this is most undoubtedly obvious, but I can't seem to
> figure it out...your help is appreciated[/color]

<snip>
[color=blue]
> 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.[/color]

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

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?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Brian Gideon
Guest
 
Posts: n/a
#3: Apr 25 '06

re: Try/Catch Array Scope Problem


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

Matt Harvey wrote:[color=blue]
> 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;
> 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?[/color]

Matt Harvey
Guest
 
Posts: n/a
#4: Apr 25 '06

re: Try/Catch Array Scope Problem


Jon Skeet [C# MVP] wrote:[color=blue]
> Matt Harvey <rifleman@gmail.com> wrote:
>[color=green]
>>The solution to this is most undoubtedly obvious, but I can't seem to
>>figure it out...your help is appreciated[/color]
>
>
> <snip>
>[color=green]
>>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.[/color]
>
>
> Assuming it gets that far. An exception could be thrown asynchronously
> before that assignment happens.
>
>[color=green]
>> If I try to initialize array1 to null when I declare it, assigning
>>values to it errors out.[/color]
>
>
> 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?
>[/color]
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.
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#5: Apr 25 '06

re: Try/Catch Array Scope Problem


Matt Harvey <rifleman@gmail.com> wrote:[color=blue]
> 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.[/color]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Closed Thread