Question is : Which can appropriately be thrown by a programmer using Java SE technology to create a desktop application?
Solution can be obtained by analyzing the existing classes.
1. NoClassDefFoundError
Any errors which are subclasses of Error should not be thrown programmatically. They are handled by JVM and Exceptions can only be handled by a programmer .
2. NullPointerException
Ex: consider the code
ArrayList<Integer> a=null;
a.add(1); // throws NullPointerException
If we analyze the code present in ArrayList.add, we don't find the method throwing NullPointerException. Similarly we don't find ClassCastException and ArrayIndexOutOfBoundsException exceptions are also not thrown explicitly in the existing Oracle JDK classes. Reason is JVM takes care of throwing the appropriate exception in such scenarios.
3. ArrayIndexOutOfBoundsException
String[] s=new String[1];
s[2]="abc"; // throws ArrayIndexOutOfBoundsException, But there is no declaration in String class
4.ClassCastException
ArrayList<Integer> a=new ArrayList<Integer>();
a.add(1);
List<Integer> list=a;
LinkedList<Integer> l=(LinkedList<Integer>)list; // throws ClassCastException, But no declaration in LinkedList class
5. NumberFormatException
double val = Double.parseDouble("abc");
Double.parseDouble explicitly throw NumberFormatException if the format of the string is unexpected.
//Following is the syntax of Double.parseDouble method
public static double parseDouble(String s) throws NumberFormatException {
return FloatingDecimal.parseDouble(s);
}
So the answer is NumberFormatException.