# using try

• ### Question

• User-1941213043 posted

I have a 2D int array and wish to traverse elements in a radius of 3 points from other specific points.

So if array[y,x] was the specific point:

for (int i = y-3, i<y+4,i++) { for (int j = x-3, j<xy+4,j++) { // LOGIC HERE }}

However since the 2d array is 25x25 dimensions, I would get an out of bound error if array[y,x] would be too close to the "border", or bounds.

What would be the best way of dealing with this? I know how to write the logic for it using if statements, but I was wondering what else I can do?

I thought of using try

```for (int i = y-3, i<y+4,i++) { for (int j = x-3, j<xy+4,j++) {

try {

//LOGIC HERE

}}}```

But I do not know if this would be proper technique, which is important to me.

Thanks!

Thursday, August 1, 2013 8:44 AM

• User1508394307 posted

You can check size of array using

myArray.GetLength(0) -> Gets first dimension size
myArray.GetLength(1) -> Gets second dimension size

`for (int i = y-3, i<y+4 && i<myArray.GetLength(0), i++) {  for (int j = x-3, j<xy+4 && j<myArray.GetLength(1),j++) { ... `
• Marked as answer by Thursday, October 7, 2021 12:00 AM
Thursday, August 1, 2013 8:54 AM
• User281315223 posted

I would highly recommend reviewing over the logic of your existing loop to help avoid these out of bounds exceptions in general such as by adding logic within your loops to ensure they don't exceed the bounds of your array using the GetLength() method:

```//Notice that you are checking the length to ensure the index isn't exceeded within your array
for (int i = y-3; i < y+4 && yourArray.GetLength(0); i++)
{
for (int j = x-3; j < xy+4 && yourArray.GetLength(1); j++)
{
}
}```

However you could use a try-catch block to handle these exceptions (and basically ignore them if they occur) as seen below :

```for (int i = y-3; i < y+4; i++)
{
for (int j = x-3; j < xy+4;j++)
{
try
{
//Attempt to perform your logic here
}
catch
{
//An error occurred - likely an Out-of-Bounds Exception you want to ignore
continue;
}
}
}```

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Thursday, August 1, 2013 8:57 AM

### All replies

• User1508394307 posted

You can check size of array using

myArray.GetLength(0) -> Gets first dimension size
myArray.GetLength(1) -> Gets second dimension size

`for (int i = y-3, i<y+4 && i<myArray.GetLength(0), i++) {  for (int j = x-3, j<xy+4 && j<myArray.GetLength(1),j++) { ... `
• Marked as answer by Thursday, October 7, 2021 12:00 AM
Thursday, August 1, 2013 8:54 AM
• User281315223 posted

I would highly recommend reviewing over the logic of your existing loop to help avoid these out of bounds exceptions in general such as by adding logic within your loops to ensure they don't exceed the bounds of your array using the GetLength() method:

```//Notice that you are checking the length to ensure the index isn't exceeded within your array
for (int i = y-3; i < y+4 && yourArray.GetLength(0); i++)
{
for (int j = x-3; j < xy+4 && yourArray.GetLength(1); j++)
{
}
}```

However you could use a try-catch block to handle these exceptions (and basically ignore them if they occur) as seen below :

```for (int i = y-3; i < y+4; i++)
{
for (int j = x-3; j < xy+4;j++)
{
try
{
//Attempt to perform your logic here
}
catch
{
//An error occurred - likely an Out-of-Bounds Exception you want to ignore
continue;
}
}
}```

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Thursday, August 1, 2013 8:57 AM
• User-1910946339 posted

Instead of directly access each cell with

array[i, j]

put the array access code in a method.  It depends on what is in the array but something like

```bool TryArray(int[] theArray, int x, int y, out int value)
{
value = 0;
if (x<0 || x > theArray.GetUpperBound(0)) return false;
if (y<0 || y > theArray.GetUpperBound(1)) return false;

value = array[x,y];
return true;
}```

In your main code you would do

```int value; \\This would be outside the loops
.
.
.
if(TryArray(array,i,j, out value)) {
// Do whatever you want with value
} else {
// It was outside the bounds so don't touch value
}```

The code could be tightened up and made more general but the basic idea is to provide a 'safe' way to access the array modeled on the exist family of TryXXX methods.

You should not use Exceptions for this sort of thing.  Exceptions should only be used for exceptional behaviour.  Of the 625 points in your array more than a third of them would cause at least one exception to be thrown - hardly exceptional.

Saturday, August 3, 2013 1:19 AM