Exception Handling in Java Part-2

Types of Exceptions in Java

In Java, a exception is categorized as:

  1. Checked Exceptions: The exceptions which are checked by the compiler to ensure the smooth execution of the program during the run time are known as checked exceptions.
  2. Unchecked Exception: The exceptions which are not checked by the compiler are called unchecked exceptions.

In checked exceptions, there are partially checked and fully checked exceptions.

Partially Checked vs Fully Checked Exceptions

A checked exception is considered to be fully checked if all of its child classes are also checked.
For example: IOException, InterruptedIOException, SQLException.
A partially checked exceptions are those exceptions if and only if some of its child classes are unchecked. There are only 2 partially checked exceptions in java. They are Exception and Throwable.


Handling the Exceptions

To handle the exceptions, try and catch blocks are used. In try block, those codes are kept which can possibly throw any kind of exceptions. Whereas in catch block, those codes are kept which will be executed if any kind of exception is thrown by the code inside the try block. Syntax:

try
{
    //code which might throw any exception
}
catch(xxx e)
{
    //handling code
}

Consider a program without exception_handling as

public class FirstExample
{
    public static void main(String []args)
    {
        System.out.println("First Line");
        System.out.println(10/0);             //line which will throw an exception
        System.out.println("Third Line");
    }
}

Output:

First Line
Exception in thread "main" java.lang.ArithmeticException: / by zero
	at FirstExample.main(FirstExample.java:6)

Now recreating the same program but with the help of try catch block, it will be

public class SecondExample
{
    public static void main(String []args)
    {
        System.out.println("First Line");
        try
        {
            System.out.println(10/0);             //line which will throw an exception
        }
        catch(ArithmeticException e)              //passing the object of exception class as argument
        {
            System.out.println("Cannot divide by 0");  //handling code
        }
        System.out.println("Third Line");
    }
}

Output:

First Line
Cannot divide by 0
Third Line

Control Flow in try catch

Consider the following program:

try
{
    State1;
    State2;
    State3;
}
catch(xxx e)
{
    State4;
}
State5;

Now possible cases with the above code can be as following:

Case 1:

If there are no exceptions then in that condition state 1, 2, 3 and 5 will be executed normally.

Case 2:

If the exceptions are thrown at state2 and the corresponding catch block catches the thrown exceptions then in that case state 1, 4 and 5 will be executed.

Case 3:

If the exceptions are thrown at state2 and the corresponding catch block could not catch it then state 1 will be only executed and the program will terminate abnormally.

Case 4:

If the exceptions are thrown at state 4 or 5 then the program will surely terminate abnormally.

Note- In a try block if any where an exceptions are thrown, rest of the lines of code will not be executed even if the exceptions are handled through the catch block. Therefore it is recommended to put only that code which might throw any exception rather than putting the complete code.


Methods to print Exception information

Inside the Throwable class, there are methods which when called prints the information of the exception thrown. These methods are:

  1. printStackTrace(): It is a method which prints the information of the exception in the following format:
    Name of an Exception: Description
            Stack Trace
  2. toString(): It is a method which prints the information in a precise manner as follows:
    Name of Exception: Description
  3. getMessage(): It is a method which prints only the description of the exception in the following manner:
    Description

To understand the working of the above methods, consider the following program:

public class ThirdExample
{
    public static void main(String []args)
    {
        try
        {
            int a=5/0;
            System.out.println("Value of a is "+a);
        }
        catch(Exception e)
        {
            System.out.println("Information by getMessage()");
            System.out.println(e.getMessage());  
            System.out.println("Information by toString()");
            System.out.println(e.toString());
            System.out.println("Information by printStackTrace()");
            e.printStackTrace();
        }
    }
}

Output:

Information by getMessage()
/ by zero
Information by toString()
java.lang.ArithmeticException: / by zero
Information by printStackTrace()
java.lang.ArithmeticException: / by zero
	at ThirdExample.main(ThirdExample.java:7)

For any query , put your comments below.

Leave a Comment

Your email address will not be published. Required fields are marked *