Learn how to handle Exception using Java

An exception is an abnormal condition which occurs during the running of a program. For example division by zero, invalid array index are the instances of exceptions. It’s better to anticipate such errors in advance and take necessary precautions while writing source code itself.

The object class is divided into a subclass called Throwable and this is again divided into Exception and Error classes.The Exception class is again classified into IOException and Runtime Exception.

Let us now take a look at how to handle the exceptions by writing codes in Java. Java handles exceptions using the following keywords try, catch, finally, throws, throw. Programmers use try catch clause in common to trap errors. The main advantage of using try catch is it investigates the errors and does not terminate the programs abruptly.

Wherever a try is used, a corresponding catch is essential. The finally clause is used so that even if no catch is matching the statements inside finally block is always executed. If you write a java program to divide a number by Zero, the compiler returns a division by zero error immediately. If you include try -catch in the program it will show you an error but will not terminate as such.

Usage of Try – Catch

The statements that are to be guarded is to be placed inside a try block. In the above instance the statements essential for dividing a number is to be placed inside a try block. The causes for the exception that will be generated is to be placed inside a catch clause.

The finally block is used after the catch statements and as mentioned earlier all the statements inside it will execute.

The following example shows the usage of Try-catch block by making use of Arithmetic Exception. Inside the try block, statements for computing division is coded and Arithmetic Exception is handled. If you compile and execute this code you will get an error message printed inside the catch block. If you substitute the value of y to 1, the result will be just fine. Hence only when an error occurs the catch block statements will be printed.

  1: class Excep { // class declaration
  2: public static void main(String args[]) {
  3: try {
  4: int x =50;
  5: int y =0;
  6: int z = x/y;
  7: System.out.println(" The answer is " +z);
  8: } catch( ArithmeticException e) {
  9: System.err.println(" You generated an exception" +e) 
 10: }
 11: }
 12: }

Save the above code as Excep.java and execute. If you include a finally statement it will also execute after the catch clause. Similarly, if there is a invalid array index the appropriate exception syntax should have to be used inside the catch statement.

Sometimes multiple catch statements are used. For instance, if an ArrayIndexOutOfBoundsException errors occurs and also a NumberFormatException error also exists you can very well include two catch statements after the try block. These statements will execute as by their occurrence.

You can use throws clause if the program does not want to handle exceptions using the try-catch clause. The exception to be generated should have to be mentioned in the programs main statement.

Some Common Exceptions Details and where used

IOException   –> General I /O

EOFException –> End of File while reading a file

FileNotFound  –> File not found

SQLException –> Database access

Unknown Host –> Host could not be located

Leave a Comment