Professional Documents
Culture Documents
6. Checked exceptions that you should know about for the exam –
java.lang.Exception, java.io.IOException extends java.lang.Exception,
java.io.FileNotFoundException extends java.io.IOException.
7. Unchecked exceptions that you should about for the exam – All the ones
mentioned below.
http://enthuware.com
Exceptions thrown by JVM
Thrown
1. java.lang.ArrayIndexOutOfBoundsException extends java.lang.RuntimeException
Thrown when attempting to access an array with an invalid index value (either negative or beyond the length
of the array).
Example :
int[] ia = new int[]{ 1, 2, 3}; // ia is of length 3.
System.out.println(ia[3]); //exception !!!
Example :
Object s = “asdf”;
StringBuffer sb = (StringBuffer) s; //exception at runtime because s is referring to a String.
Example :
String s = null;
System.out.println(s.length()); //NullPointerException at runtime because s is null.
Example :
public class X { int k = 0;
public static void main(String[] args){
k = 10/0; //throws java.lang.ArithmeticException
}
}
Example:
Example :
public class X { int k = 0;
static{
k = 10/0; //throws java.lang.ArithmeticException but this is wrapped into a
//ExceptionInInitializationError and thrown outside.
}
}
http://enthuware.com
7. java.lang.StackOverflowError extends java.lang.Error
Thrown when the stack is full. Usually thrown when a method calls itself and there is no boundary
condition.
Example :
public void m1(int k){
m1(k++); // exception at runtime.
}
Example :
Object o = new com.abc.SomeClassThatIsNotAvailableInClassPathAtRunTime(); // exception at runtime.
Example:
public void processData(byte[] data, int datatype)
{
if(datatype != 1 || datatype != 2) throw new IllegalArgumentException();
else …
}
Example:
Connection c = …
public void useConnection()
{
if(c.isClosed()) throw new IllegalStateException();
else …
}
http://enthuware.com
Example:
Integer.parseInt(“asdf”);
http://enthuware.com