//....

int vals[] = new int [12];
vals[18] = 10;
catch (ArrayIndexOutofBoundsException) {

//handle error

...
}

youve only made your array size 12 as in
int vals[0]
int vals[1]
int vals[2]
int vals[3]
int vals[4]
int vals[5]
int vals[6]
int vals[7]
int vals[8]
int vals[9]
int vals[10]
int vals[11]

so you can have vals[18]

at least the exception will be handled :)

You also need to enclose it in a try/catch block

try {
   int vals[] = new int [12]; 
   vals[18] = 10; 
} catch (ArrayIndexOutofBoundsException) { 
//handle error 
}

Note - since you create the vals[] array in the try/catch block the vals[] array will not be available after the try/catch (see scope documentation on try/catch)

To avoid this declare your member variables outside the block:

int vals[] = new int [12]; 
try {
   vals[18] = 10; 
} catch (ArrayIndexOutofBoundsException) { 
//handle error 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.