473,386 Members | 1,741 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Try/Catch Array Scope Problem

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?
Apr 25 '06 #1
4 2415
Matt Harvey <ri******@gmail.com> wrote:
The solution to this is most undoubtedly obvious, but I can't seem to
figure it out...your help is appreciated
<snip>
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?

--
Jon Skeet - <sk***@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
Apr 25 '06 #2
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:
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?


Apr 25 '06 #3
Jon Skeet [C# MVP] wrote:
Matt Harvey <ri******@gmail.com> wrote:
The solution to this is most undoubtedly obvious, but I can't seem to
figure it out...your help is appreciated

<snip>
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.
Apr 25 '06 #4
Matt Harvey <ri******@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@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
Apr 25 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alejandro Narancio | last post by:
Hello my name is Alejandro and i've a question. I need to make a functions with this characteristics: - params: an array of strings with the names of components in a page (for example, some...
10
by: Bungle | last post by:
Hi I am trying to do something really simple. Create a method within a class which will connect to the database, pull back a result and return it out the method. Not hard. All my database...
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
11
by: Terry Olsen | last post by:
How can I catch a right-click on a DropDownMenuItem?
34
by: Bob | last post by:
Hi, The compiler gives Warning 96 Variable 'cmdSource' is used before it has been assigned a value. A null reference exception could result at runtime. Dim cmdSource as SQlClient.SQLDataReader...
3
by: John Rivers | last post by:
try catch blocks create a new scope i don't think that they should for example, to add error handling to this statement: int x = 1; you have to rewrite it as a seperate declaration and...
28
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
15
by: Steve | last post by:
I am having problems getting values out of an array. The array is set as a global array and values are pushed into it as they are read from a JSON file using a "for loop". When the "for loop" is...
25
by: biplab | last post by:
Hi all, I am using TC 3.0..there if I declare a integer array with dimension 162*219...an error msg saying that too long array is shown....what should I do to recover from this problem???
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.