Java Programming
About Lesson

Java providing pre-defined classes to handle the exceptions.  These classes provide various methods to handle exceptions by creating their respective objects.  All the exception classes are the child classes are Throwable class.  The Throwable class has two direct child classes known as Exception and Error.

The following diagram shows the hierarchy of exception classes of java language:

 

 

Diagram 2:

 

Exception and Error classes and their sub classes provide various pre-defined methods and properties to handle the exceptions by creating their respective objects.

 

Exception Vs Error
The main difference between Exception and Error is, the Exception can be handled and recovered but the Error cannot be handled and even you try to handle, it cannot be recovered.

 

Table showing some of popular pre-defined Exception and their meaning

Exception

Meaning

Arithmetic Exception

Occurred when Division by zero

ArrayOutOfBoundsException

Array is used with invalid index

ArrayStoreException

Array is assigned with different data type value rather than its own data type.

IllegalArgumentException

When arguments for a method are given wrong

NegativeArraySizeException

When array is declared with negative index value

SecurityException

When there is a breach to the security

ClassNotFoundException

When the specified class is not found

Interrupted Exception

When a thread has been stopped forcefully

 

Some useful methods defined under Throwable class, to print exception information on the console:
printStackTrace(): This method prints name of the exception, description along with complete stack trace information

//Example code using printStackTrace() method
public static void main(String[] args) {
try{
System.out.println(50/0);
}
catch(ArithmeticException e){
e.printStackTrace();
}
}

Output:
java.lang.ArithmeticException: / by zero
at PkgName.ClassName.main(ClassName.java:7)

In the above output
Name of the exception: java.lang.ArithmeticException
Description: / by zero
Stack trace: PkgName.ClassName.main(ClassName.java:7)

toString(): This method prints name of the exception with description only without any stack information. Whenever you are trying to print the reference of any object using println method toString() method will be called automatically. Hence, using toString() method is optional.
//Example code using printStackTrace() method
public static void main(String[] args) {
try{
System.out.println(50/0);
}
catch(ArithmeticException e){
System.out.println(e.toString()); //System.out.println(e);
}
}

Output:
java.lang.ArithmeticException: / by zero

getMessage(): this method is used to print description of exception only without any additional information.
//Example code using printStackTrace() method
public static void main(String[] args) {
try{
System.out.println(50/0);
}
catch(ArithmeticException e){
System.out.println(e.getMessage());
}
}

Output:
/ by zero

//Example code using ArithmeticException
import java.util.Scanner;

public class ArithExceptEg {

public static void main(String[] args) {
int n,d,res=0;

System.out.println(“Enter Numerator:”);
Scanner sc=new Scanner(System.in);

n=sc.nextInt();

System.out.println(“Enter Denominator as Non-zero:”);
d=sc.nextInt();

try
{
res=n/d;
}
catch(ArithmeticException e)
{
System.out.println(“Exception Handled”);
System.out.println(“Details of Exception are:”);
System.out.println(e.getMessage());

}

System.out.println(“Result is :”+res);

System.out.println(“Rest of the code”);

}

}

//Example using NullPointerException
public class NullPointerExcepEg {

public static void main(String[] args) {
String s=null;
int len=0;

try
{
len=s.length();
}
catch(NullPointerException e)
{
e.printStackTrace();
}

System.out.println(“Length of string is :”+len);

}

}

//Example using NumberFormatException
public class NumberFormatExceptEg {

public static void main(String[] args) {
String s=”Best”;

int i=0;

try
{
i=Integer.parseInt(s);
}
catch(NumberFormatException e)
{
System.out.println(e.toString());
}
System.out.println(“value of i is:”+i);

}

}

//Example using ArrayIndexOutOfBoundsException
public class ArrayIndexExcepEg {

public static void main(String[] args) {
int a[]=new int[5];

try
{
a[10]=50;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}

System.out.println(“Array Elements are:”);
for(int i=0;i<5;i++)
{
System.out.print(a[i]+” “);
}

}

}

You cannot copy content of this page