There are two types of exception,
Built-in exception
User Defined Exception
Built-in exception
The below given are the list of build in exception for handling some error in java library.
Arithmetic Exception It is thrown when an exceptional condition has occurred in an arithmetic operation.
public class Arith_exception {
public static void main(String[] args) {
try{
int a = 12;
int b = 0;
int c = a/b;
}
catch (ArithmeticException e){
System.out.println(e);
}
}
}
a
java.lang.ArithmeticException: / by zero
Process finished with exit code 0
ArrayIndexOutOfBoundException It is 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.
public class ArrayIndexOutofBound {
public static void main(String args[])
{
try{
int a[] = new int[5];
a[6] = 9;
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println (e);
}
}
}
a
java.lang.ArrayIndexOutOfBoundsException: 6
Process finished with exit code 0
ClassNotFoundException This Exception is raised when we try to access a class whose definition is not found
FileNotFoundException This Exception is raised when a file is not accessible or does not open.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class FileNotFound_exception {
public static void main(String args[]) {
try {
File file = new File("E://file.txt");
FileReader fr = new FileReader(file);
} catch (FileNotFoundException e) {
System.out.println(e);
}
}
}
a
java.io.FileNotFoundException: E:\file.txt (The system cannot find the path specified)
Process finished with exit code 0
IOException It is thrown when an input-output operation failed or interrupted
InterruptedException It is thrown when a thread is waiting , sleeping , or doing some processing , and it is interrupted.
NoSuchFieldException It is thrown when a class does not contain the field (or variable) specified
NoSuchMethodException It is thrown when accessing a method which is not found.
NullPointerException This exception is raised when referring to the members of a null object. Null represents nothing.
public class Null_exception {
public static void main(String args[])
{
try {
String a = null;
System.out.println(a.charAt(0));
} catch(NullPointerException e) {
System.out.println(e);
}
}
}
a
java.lang.NullPointerException
Process finished with exit code 0
NumberFormatException This exception is raised when a method could not convert a string into a numeric format.
public class NummberFormat_exception {
public static void main(String args[])
{
try {
int num = Integer.parseInt ("akki") ;
System.out.println(num);
} catch(NumberFormatException e) {
System.out.println(e);
}
}
}
a
java.lang.NumberFormatException: For input string: "akki"
Process finished with exit code 0
RuntimeException This represents any exception which occurs during runtime.
StringIndexOutOfBoundsException It is thrown by String class methods to indicate that an index is either negative than the size of the string
public class StringIndexOutofBound_exception {
public static void main(String args[])
{
try {
String a = "This is like chipping ";
char c = a.charAt(24);
System.out.println(c);
}
catch(StringIndexOutOfBoundsException e) {
System.out.println(e);
}
}
}
a
java.lang.StringIndexOutOfBoundsException: String index out of range: 24
Process finished with exit code 0
User Defined Exception
In order for user to handle error based on him\her problem, Java allow us to define our own exception. Learn how to define then and use then based on below instructions.
public class UserDefined_exception extends Exception {
public static void main(String[] args)
{
int b = 999;
try {
if (b < 1000)
{
UserDefined_exception me =
new UserDefined_exception();
throw me;
}
}
catch (UserDefined_exception e) {
e.printStackTrace();
}
}
}
a
Exception.UserDefined_exception
at Exception.UserDefined_exception.main(UserDefined_exception.java:12)
Process finished with exit code 0