Jagged Arrays IndexOutOfRangeException

Find which index was out of range?

Page 1 of 1

2 Replies - 661 Views - Last Post: 14 April 2010 - 12:21 PM Rate Topic: -----

#1 reaper4334  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 107
  • Joined: 01-November 09

Jagged Arrays IndexOutOfRangeException

Posted 14 April 2010 - 11:37 AM

I'm using a jagged array in C# and I'm checking for an index out of range exception.
This all works fine, except I want to know if it was out of range in x or y, for example..

// Declaring the array
int[][] myArray;
myArray = new int[10][];
for (int i = 0; i < 10; i++)
	{
	myArray[i] = new int[10];
	}
	
try{
	if (myArray[x][y] != 0)
		{
		myFunction();
		}
	}
catch (IndexOutOfRangeException ex){
	// Handle the exception
	// Determine which of x and y was raised the exception
	}


(that's just a quick example of my problem, not my actual code - I figured it may make it easier if it's more generic)

I'd appreciate any help whatsoever. Thanks, Reaper4334.

Is This A Good Question/Topic? 0
  • +

Replies To: Jagged Arrays IndexOutOfRangeException

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,599
  • Joined: 25-December 08

Re: Jagged Arrays IndexOutOfRangeException

Posted 14 April 2010 - 12:06 PM

in your catch block you could do something like:
if( y >= myArray[o].Length ){
  Console.WriteLine(" Blame Y");
}else{
  Console.WriteLin( "Blame X");
}


Was This Post Helpful? 0
  • +
  • -

#3 reaper4334  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 107
  • Joined: 01-November 09

Re: Jagged Arrays IndexOutOfRangeException

Posted 14 April 2010 - 12:21 PM

View PostSwiftStriker00, on 14 April 2010 - 11:06 AM, said:

in your catch block you could do something like:
if( y >= myArray[o].Length ){
  Console.WriteLine(" Blame Y");
}else{
  Console.WriteLin( "Blame X");
}



Thanks very much. To be honest I'm slightly ashamed I didn't think of doing that myself ^^
I resorted to having a pointless conditional statement with X and catching the another exception.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1