Exception in finding a string in a 2D array

I have a function where I must try to find a string in a 2D array of strings and obtain the row and column which I found the string. My code is as follows, but I get an error when I try to run it (not compile, run.) I can't figure out the error, help please?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
void findItem (int &row, int &column, string arr[20][40], string item) 
{				/*function: findItem
				   purpose: attempt to find an item and pass by parameter the row and column of the item if found. If not found, passes -1,-1
				   Variables are self-explanatory.
				 */
  
row = -1;  column = -1;
  
for (int i = 0; i < 20; i++)
    
    {
      for (int j = 0; j < 40; j++)
	
	{
	  if (arr[i][j] == item) //successfully runs
	  {
		  try{row = i;column = j;
		  throw row;
		  }
		  catch(int i)
		  {cout<< "Error at row "<< i<<endl;} //error is caught at the row of the string
	  }
	}
    
}

}
Last edited on
What kind of error? Do you mean the error message that you print on line 21? Well, of course that message will get printed for each position in the 2D array where you find a string that is equal to item.
Last edited on
how?? doesn't "catch" only occur when there is an error? The executable immediately starts freezing up when I reach that line.
The catch block gets executed whenever there is an exception of a matching type thrown from inside the try block. You are always throwing an exception on line 18 so the error message on line 21 will always get executed. It doesn't explain why it is freezing though.
Last edited on
What's the point of the try-catch at all?
Just set row to i, col to j and return;
only occur when there is an error?

What error?

What error do you expect to possibly happen in your function?
Topic archived. No new replies allowed.