Exception Classes in Java
Exception Classes in Java
Exception are mainly classified into two type checked exception and un-checked exception.
Checked Exception Classes
- FileNotFoundException
- ClassNotFoundException
- IOException
- InterruptedException
Un-Checked Exception Classes
- ArithmeticException
- ArrayIndexOutOfBoundsException
- StringIndexOutOfBoundsException
- NumberFormateException
- NullPointerException
- NoSuchMethodException
- NoSuchFieldException
FileNotFoundException
If the given filename is not available in a specific location ( in file handling concept) then FileNotFoundException will be raised. This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors.
ClassNotFoundException
If the given class name is not existing at the time of compilation or running of program then ClassNotFoundException will be raised. In other words this exception is occured when an application tries to load a class but no definition for the specified class name could be found.
IOException
This is exception is raised whenever problem occurred while writing and reading the data in the file. This exception is occurred due to following reason;
- When try to transfer more data but less data are present.
- When try to read data which is corrupted.
- When try to write on file but file is read only.
InterruptedException
This exception is raised whenever one thread is disturb the other thread. In other words this exception is thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity.
ArithmeticException
This exception is raised because of problem in arithmetic operation like divide by zero. In other words this exception is thrown when an exceptional arithmetic condition has occurred. For example, an integer "divide by zero".
Example
class ExceptionDemo { public static void main(String[] args) { int a=10, ans=0; try { ans=a/0; } catch (Exception e) { System.out.println("Denominator not be zero"); } } }
ArrayIndexOutOfBoundsException
This exception will be raised whenever given index value of an array is out of range. The index is either negative or greater than or equal to the size of the array.
Example
int a[]=new int[5]; a[10]=100; //ArrayIndexOutOfBoundsException
StringIndexOutOfBoundsException
This exception will be raised whenever given index value of string is out of range. The index is either negative or greater than or equal to the size of the array.
Example
String s="Hello"; s.charAt(3); s.charAt(10); // Exception raised
chatAt() is a predefined method of string class used to get the individual characters based on index value.
NumberFormateException
This exception will be raised whenever you trying to store any input value in the un-authorized datatype.
Example: Storing string value into int datatype.
Example
int a; a="Hello";
Example
String s="hello"; int i=Integer.parseInt(s);//NumberFormatException
NoSuchMethodException
This exception will be raised whenever calling method is not existing in the program.
NullPointerException
A NullPointerException is thrown when an application is trying to use or access an object whose reference equals to null.
Example
String s=null; System.out.println(s.length());//NullPointerException
StackOverFlowException
This exception throw when full the stack because the recursion method are stored in stack area.