Java-Exception Handling

Exception Handling

The errors that occur during the execution of a program are known as run-time errors. They may generate incorrect output or terminate the execution of a program abruptly. Some causes for the occurrence of run-time errors are division of a value by zero, incorrect usage of an array index and incorrect specification of number format. An exception is an abnormal condition that is caused by a run-time error in the program. Java enables us to smoothly handle the various types of exceptions that may occur during the execution of a program with the help of its well-defined exception handling mechanism.

Default Exception Handling

When an exception occurs during the execution of a program, the JVM checks whether any user-defined exception handling mechanism is defined in that program. If no such user-defined exception handling mechanism is present, the JVM will automatically make use of the default exception handling mechanism for reporting the exception that has occurred. The following program illustrates this concept.

The following program is designed to wantonly generate an exception by dividing an integer by zero. When the JVM tries to execute this division, it stops the execution in order to deal with this error condition. As this program does not involve any user-defined exception handling mechanism, the JVM will invoke the default exception handling mechanism. As a result, the exception will be reported in the pre-defined format.

Program

Admittedly, the format of the above exception report is not user-friendly. But it is easy to decipher it. The name of the exception that has occurred is displayed in the first line. The line number of the statement that caused the exception is displayed in the second line, as 6.

User-defined Exception Handling Mechanism

The default exception handling mechanism has the following two drawbacks :

  1.  It always reports the exception in a predefined format, which may not be always preferable.
  2.  After reporting an exception, it immediately terminates the execution of the program. 

The user-defined exception handling mechanism does not have the above mentioned two drawbacks. It shall be instructed to report the error condition in any special format that we prefer. It will automatically resume the execution of a program after reporting an error condition.

Syntax of User-Defined Exception Handling Mechanism

Java uses a keyword try to preface a block of code that is likely to cause an error condition and “throw” an exception. A catch block defined by the keyword catch “catches” the exception “thrown” by thee try block and handles it appropriately. The catch block is added immediately after the try block. The following example illustrates the use of simple try and catch statements :

The try block can have one or more statements that could generate an exception. If any one statement generates an exception, the remaining statements in the block are skipped and execution jumps to the catch block that is placed next to try block. The catch block too can have one or more statements that are necessary to process the exception. Remember that every try statement should be followed by at least one catch statement; otherwise compilation error will occur.

Note that the catch statement works like a method definition. The catch statement is passed a single parameter, which is reference to the exception object thrown (by the try block). If the catch parameter matches with the type of exception object, then the exception is caught and statements in the catch block will be executed. Otherwise, the exception is not caught and the default exception handler will cause the execution to terminate.

The following program illustrates the use of try and catch blocks to handle an arithmetic exception. Note that program is a modified version of the previous program.

Program

final2

Note that program did not stop at the point of exceptional condition. It catches the error condition, prints the error message, and then continues the execution, as if nothing has happened, Compare with the output of previous program which did not print the complete message.

Program Source : Without User-defined Exception

class Javaapp {

    public static void main(String[] args) {
        
        int a=1/0;
        System.out.println("Execution Complete");
    }
}

Program Source : With User-defined Exception

class Javaapp {

    public static void main(String[] args) {
        
        try{
            int a=1/0;
        }catch(ArithmeticException ex)
        {
            System.out.println("Division by zero");
        }
        System.out.println("Execution Complete");
    }
}

Leave a Comment