Hi Joel,
Whenever if you take the input from a user then we cannot anticipate what is he/she going to provide....a zero or a non zero value,So rather then
testing each time whether the value inputted is 0 or not(testing does take some time,and it can be at run time and not compile time if input is provided at the run time itself)So to avoid this we can go for the try ...catch block wherein the catch block gets executed only when the input is zero.
Now consider checking for ArrayIndexOutofBounds()... shall we check by a if condition that is the range outside the size or will we put a try ....catch block around the code.
class Test
{
int items[10];
int give;
Test()
{
//Put items in array named items from a[0] to a[10]
foo();
}
public int foo()
{give=0//Default value
//Ask user which one he wants to remove
//take input from user and put in variable selected
//Now do we check whetehr selected is in range 0 to 10 or
try
{
give=items[selected];
}catch(ArrayIndexOutfBounds(Exception e){System.out.println("Sorry Wrong value so be happy with the default");}
return give;
}
public static void main(
String a[])
{
new Test();
}
}