Exception Handling In Java

When there is an exception, the user data may be corrupted. This should be tackled by the programmer by carefully designing the program.

Steps To Follow

The corrupted data can be tackled by the programmer by performing the following steps

Step 1: Try Block

  • The programmer should observe the statements in his program where there may be a possibility of exceptions. Such statements should be written inside a try block. The greatness of try block is that even if some exception arises inside it, the program will not be terminated. When JVM understands that there is an exception, it stores the exception details in an exception stack and then jumps into a catch block. A try block looks as follows
try
{
  statements;
}

Step 2: Catch Block

  • The programmer should write the catch block where he should display the exception details to the user. This helps the user to understand that there is some error in the program. The programmer should also display a message regarding what can be done to avoid this error. Catch block looks like this
catch(Exceptionclass ref)
{
  statements;
}

The reference ref above is automatically adjusted to refer to the exception stack where the details of the exception are available, we can display the exception details using any one of the following uses

  • Using print() or println() methods, such as System.out.println(ref);
  • Using printStackTrace() method of Throwable class, which fetches exception details from the exception stack and displays them.

Step 3: Finally Block

Lastly, the programmer should perform clean up operation like closing the files and terminating the threads. The programmer should write this code in the finally block. Finally block looks like this

finally
{
  statements;
}

The specialty of finally block is that the statements inside the finally block are executed irrespective of whether there is an exception or not. This ensures that all the opened files are properly closed and all the running threads are properly terminated. So, the data in the files will not be corrupted and the user is at the safe-side.

Program Using ’try’, ‘catch’ and ‘finally’ block

// Exception handling using try, catch and finally blocks
class Ex {
    public static void main(String[] args) {
        try {
            //open the files
            System.out.println("Open files");
            //do somme processing
            int n = args.length;
            System.out.println("n= " + n);
            int a = 45 / n;
            System.out.println("a= " + a);
        } catch (ArithmeticException ae) {
            //display the exception details
            System.out.println(ae);

            //display any message to the user
            System.out.println("Please pass data while running this program");

        } finally {
            //close the files
            System.out.println("Close files");
        }
    }
}

The output is

Open files
n= 0
java.lang.ArithmeticException: / by zero
Please pass data while running this program
Close files

In the above program, the try block contains the statements where there is possibility foe an exception. When there is an exception, JVM jumps into catch block. The finally block is executed even if there is exception or not. Observe the output of the program. When there is no exception, we can see normal execution and the files are closed properly, but when there is an exception, the exception details are displayed along with a message to the user. Then, finally block is executed which closes the files. This prevents any loss to user data.

Subscribe For More Content