Which three are bad practices?
A.
Checking for ArrayindexoutofBoundsException when iterating through an array to determine
when all elements have been visited
B.
Checking for Error and. If necessary, restarting the program to ensure that users are unaware
problems
C.
Checking for FileNotFoundException to inform a user that a filename entered is not valid
D.
Checking for ArrayIndexoutofBoundsExcepcion and ensuring that the program can recover if one
occur
E.
Checking for an IOException and ensuring that the program can recover if one occurs
Explanation:
A, E: Better to check if the index is within bounds.
B: Restarting the program would not be a good practice. It should be done as a last possibility only.
Incorrect answers:
Checking for FileNotFoundException and IOException exceptions are good practices.
Wrong answers highlighted, should be A,B,D
Correct answers are A,B and D.
A B D are correct answers
http://www.tutorialspoint.com/javaexamples/exception_multiple1.htm
Should be to catch multiple exception methods by using System.err.println() method of System class .
public class Main {
public static void main (String args[]) {
int array[]={20,20,40};
int num1=15,num2=10;
int result=10;
try {
result = num1/num2;
System.out.println(“The result is” +result);
for(int i =5;i >=0; i–) {
System.out.println
(“The value of array is” +array[i]);
}
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println(“Array is out of Bounds”+e);
}
catch (ArithmeticException e) {
System.out.println (“Can’t divide by Zero”+e);
}
}
}
Result:
The above code sample will produce the following result.
The result is1
Array is out of Boundsjava.lang.ArrayIndexOutOfBoundsException
: 5
a,b,d