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.

READ  Azure Services Revised Pricing Structure Announced

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.

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">  1: <span style="color: #0000ff">class</span> Excep { <span style="color: #008000">// class declaration</span>
  2: <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String args[]) {
  3: <span style="color: #0000ff">try</span> {
  4: <span style="color: #0000ff">int</span> x =50;
  5: <span style="color: #0000ff">int</span> y =0;
  6: <span style="color: #0000ff">int</span> z = x/y;
  7: System.out.println(&quot;<span style="color: #8b0000"> The answer is </span>&quot; +z);
  8: } <span style="color: #0000ff">catch</span>( ArithmeticException e) {
  9: System.err.println(&quot;<span style="color: #8b0000"> You generated an exception</span>&quot; +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.

READ  Microsoft Updated on Azure Storage Service Interruption

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 Reply

Your email address will not be published.