Site Search:

"Recognize common exception classes (such as NullPointerException, ArithmeticExcpetion, ArrayIndexOutOfBoundsException, ClassCastException)"


Back OCAJP


Sorry, yes it is, you need to remember these Exceptions for OCAJP.

OCAJP will test the following RuntimeExceptiontions. We will use an example program to show when they are thrown.





All throwable can be thrown by programmer, however, some throwable such as IOException and NumberFormatException are not thrown by JVM, they are generated by java program code; other throwable such as NullPointerException ArrayIndexOutOfBoundsException, ClassCastException, ExceptionInInitializerError are generated by JVM.


OCAJP>cat test.java 
import java.util.*;
class test {
  public static void main(String...args) {
    test t = new test();
    t.tester(args[0]);
  }
  public void tester(String a) {
    try{
       switch(a) {
         case "ArithmeticException":
           int i = 10/0;
         case "NullPointerException":
           throw null;
         case "ClassCastException":
           String x = (String)(Object)(new Integer(1));
         case "ArrayIndexOutOfBoundsException":
           int[] arr = new int[0];
           System.out.println(arr[1]);
         case "IllegalArgumentException":
           throw new IllegalArgumentException("I expect a number above 0, you gave me -10!");
         case "NumberFormatException":
           Integer.parseInt("int");
         default:
           throw new IllegalArgumentException("expect a known OCAJP RuntimeException type");
       }
    } catch(RuntimeException r) {
      r.printStackTrace();
    }
  }
}
OCAJP>javac test.java 
OCAJP>java test
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at test.main(test.java:5)
OCAJP>java test ArithmeticException
java.lang.ArithmeticException: / by zero
at test.tester(test.java:11)
at test.main(test.java:5)
OCAJP>java test NullPointerException
java.lang.NullPointerException
at test.tester(test.java:13)
at test.main(test.java:5)
OCAJP>java test ClassCastException
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at test.tester(test.java:15)
at test.main(test.java:5)
OCAJP>java test ArrayIndexOutOfBoundsException
java.lang.ArrayIndexOutOfBoundsException: 1
at test.tester(test.java:18)
at test.main(test.java:5)
OCAJP>java test IllegalArgumentException
java.lang.IllegalArgumentException: I expect a number above 0, you gave me -10!
at test.tester(test.java:20)
at test.main(test.java:5)
OCAJP>java test NumberFormatException
java.lang.NumberFormatException: For input string: "int"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at test.tester(test.java:22)
at test.main(test.java:5)
OCAJP>java test WhateverException
java.lang.IllegalArgumentException: expect a known OCAJP RuntimeException type
at test.tester(test.java:24)
at test.main(test.java:5)




There are only two checked Exceptions for OCAJP:

java.io.FileNotFoundException is subclass of java.io.IOException. Both are thrown programmatically when code have problem reading or writing a file. FileNotFoundException is specific to missing a file.


There are 3 Errors for OCAJP:
ExceptionInInitializerError  Thrown by the JVM when a static initializer throws an exception and doesn't handle.



StackOverflowError Thrown by JVM when a stack overflow occurs because an application recurses too deeply.





  • java.lang.Object

  • NoClassDefFoundError Thrown by JVM when the searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.


    Back OCAJP