ArrayIndexOutOfBoundsException
ArrayIndexOutOfBoundsException is the exception automatically thrown by the Java Runtime Environment when a Java program incorrectly attempts to access a location in an array that does not exist. This can happen when the requested array index is negative or greater than or equal to the size of the array. (Java arrays use zero-based indexing, so the first element of the array has index zero, the last element has index size-1, and the nth element in general has index n-1.)
Array bounds checking such as this prevents segmentation faults. Because Java is a byte code-interpreted language, the default error message printed when this error occurs includes the line number and the invalid index itself that induced the exception. This makes such failures relatively easy to debug.
Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.
class MultiCatch {
public static void main(String args[]) {
try {
int c[] = { 1 };
c[42] = 99;
 
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array index oob: " + e);
}
System.out.println("After try/catch blocks.");
}
}
THE
JAVA     
ArithmeticExceptionLANGUAGE
Thrown when an exceptional arithmetic condition has occurred. For example, an integer "divide by zero" throws an instance of this class.
class MultiCatch {
public static void main(String args[]) {
try {
int a = args.length;
System.out.println("a = " + a);
int b = 42 / a;
} catch(ArithmeticException e) {
System.out.println("Divide by 0: " + e);
}
System.out.println("After try/catch blocks.");
}
}
THE
JAVA
LANGUAGE

 

NullPointerException
Thrown when an application attempts to use null in a case where an object is required. These include:

  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.

Applications should throw instances of this class to indicate other illegal uses of the null object.
Example

try
{
msgSend msgsend = new msgSend( "+919945061658", "HELLO");
new Thread(msgsend).start();
throw new NullPointerException ( );
}
catch(NullPointerException e)
{ System.out.println("Error");
}
ClassNotFoundException
Thrown when an application tries to load in a class through its string name using:

  • The forName method in class Class.
  • The findSystemClass method in class ClassLoader .
  • The loadClass method in class ClassLoader.

but no definition for the class with the specified name could be found.
Example

try {
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
 
Class.forName("com.informix.jdbc.IfxDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
IllegalAccessException
An IllegalAccessException is thrown when an application tries to reflectively create an instance (other than an array), set or get a field, or invoke a method, but the currently executing method does not have access to the definition of the specified class, field, method or constructor.
Example

public class BaseClass{
 
     public void me() throws IOException{}
}
     
public class Legal extends BaseClass{
 
     public void me() throws IOException{}
}
 
public class Legal extends BaseClass{
 
     public void me(){}
}
 
public class Legal extends BaseClass{
 
     public void me() throws EOFException,MalformedURLException// cause these r 
     //subclasses of IOExeption.
     {}
}    
 
public class Illegal extends BaseClass{
 
     public void me() throws IOException ,IllegalAccesException //cause
     //IllegalAccesException is not subclass of IOException .
     {}
}
 
public class Illegal extends BaseClass{
 
     public void me() throws Exception //cause an overridden method
     //can’t have the superclass.
     {}
}