Exceptions in Java

There are many exceptions in Java. We can categorize them into two categories.

  1. Checked Exceptions (Which we can handle)
  2. UnChecked Exceptions (Which we can not handle- Run time)

Now let’s talk about each exception in Java.

Checked Exceptions: Remember that checked exceptions means the exceptions which we can handle. Few exceptions give error at compile time itself. Few exceptions doesn’t give exception at compile time but give at run time. But remember that what ever exceptions we can handle are checked exceptions. We do arithmetic operations and there might be a possibility to get exception if we pass zero to divide operation. So we can predict this error and can handle it with a catch block ArithmeticException or using throws ArithmeticException . We pass some values and execute some code and we have to predict what if the value passed is Null then we have to handle that using NullPointerException. So like these we have to predict the possibilities of getting and exception and have to handle them. If we can not predict the exceptions/ can not handle them then those are unchecked exceptions.

1. NoClassDefFoundException:

If the class reference is not available at runtime (though it is present at compile time and some how got deleted after that) then this error occurs. So this un cached exception as we can’t handle this exception.

2. ArithmeticException: Arithmetic error, such as divide-by-zero.

public static void main(String[] args) {
ArithMethod(40,0);
}
private static void ArithMethod(int a, int b) {
try{
int c = a/b;
}
catch(ArithmeticException ae){
System.out.println("Arithmetic Exception caught..");
}
}

3. NumberFormatException: Invalid conversion of a string to a numeric format.


try{
String v ="12j";
Integer inte = Integer.parseInt(v);
System.out.println("inte:"+inte);
}
catch(NumberFormatException nume){
System.out.println("NumberFormatException Caught...");
}

4. SecurityException: Attempt to violate security.

5. SqlException

6. ArrayIndexOutOfBoundsException: Array index is out-of-bounds


public static void main(String[] args) {

arrayBound();
}

private static void arrayBound() {
try{
int[] g = new int[3];
g[0] = 32;
g[1] = 26;
g[2] = 43;
g[3] = 61;
}
catch(ArrayIndexOutOfBoundsException AIOe){
System.out.println("ArrayIndexOutOfBoundsException Caught...");
}
}

7. StringIndexOutOfBoundsException: 

8. IndexOutOfBoundsException: Some type of index is out-of-bounds.

9. NullPointerException: Invalid use of a null reference.

Eg:


String s3=null;
try{
String s31 = new String(s3);
}
catch(Exception ne){
System.out.println("Null Pointer Exception Caught..");
}

I’ve listed few mostly used exceptions and There are few more catched exceptions in Java.

UnChecked Exceptions:

1. ClassNotFoundException:

If the class reference is not available at the time of compilation then this error occurs. So this is a compile time exception.

2. CloneNotSupportedException

Attempt to clone an object that does not implement the Cloneable interface.

3. IllegalAccessException

Access to a class is denied.

4. InstantiationException

Attempt to create an object of an abstract class or interface.

5. InterruptedException

One thread has been interrupted by another thread.

6. NoSuchFieldException

A requested field does not exist.

7. NoSuchMethodException

A requested method does not exist.

8. ResourceNotFoundException:

9. ResourceUnAvailableException:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s