1. Arithmetic Exception
2. NullPointerException
3. StringIndexOutOfBoundsException
4. ArrayIndexOutOfBoundsException
5. NumberFormatException
6. ClassCastException
7. NoSuchElementExcepiton
8. ClassNotFoundException
9. FileNotFoundException
10. IllegalArgumentException
11. IllegalStateException
12. IllegalAccessException
int numerator=4;
int denominator=0;
int result=numerator/denominator;//ArithmeticException
|
String name=null;
char result=name.charAt(0);//NullPointerException
|
String name1="";
char result1=name1.charAt(0);//StringIndexOutOfBoundsException
|
4. ArrayIndexOutOfBoundsException
int id[]=new int[3]; id[4]=200; //ArrayIndexOutOfBoundsException |
5. NumberFormatException
String name="james"; int i=Integer.parseInt(name);//NumberFormatException |
6. ClassCastException
Object object = Integer.valueOf(200); String name = (String)object; // ClassCastException . |
Parent p = new Parent(); Child c = new Child(); c = p; //ClassCastException. |
TreeSet treeSet=new TreeSet<>(); treeSet.add(200); treeSet.add("name"); System.out.println(treeSet); |
7. NoSuchElementExcepiton
Hashtable htable = new Hashtable(); Enumeration enumeration = htable.elements(); enumeration.nextElement(); //java.util.NoSuchElementExcepiton here because enumeration is empty |
HashMap htable = new HashMap(); Iterator itr = htable.keySet().iterator(); itr.next(); //java.util.NoSuchElementExcepiton here because iterator is empty |
StringTokenizer stoken = new StringTokenizer("", ":");
System.out.println(stoken.nextToken());
|
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root"); //java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
|
9. FileNotFoundException
private static final String filename = "emp.txt"; BufferedReader rd = null; rd = new BufferedReader(new FileReader(new File(filename))); String inputLine = null; while((inputLine = rd.readLine()) != null) { System.out.println(inputLine); } |
10. IllegalArgumentException
int value; public Test(int value) { if (value < 0 || value > 100) { throw new IllegalArgumentException(Integer.toString(value)); } } Test t = new Test(121); System.out.println(t.value); |
11. IllegalStateException
while(it1.hasNext()) { // Object obj1 = it1.next(); it1.remove(); } calling it1.remove() without calling it1.next() is an exception. The following code throws IllegalStateException. |
12. IllegalAccessException
Set<String> myStr = new HashSet<String>(); myStr.add("obj1"); Iterator itr = myStr.iterator(); Method mtd = itr.getClass().getMethod("hasNext"); System.out.println(m.invoke(it)); Solution: Method mtd= itr.getClass().getMethod("hasNext"); mtd.setAccessible(true); |