Exception Handling

Posted on

 Exception is an alternate name for an error. Errors are always likely to occer and it is important to provide means to handle them. Conventional languages such as C do not found ect., In such cases the program is abnormally terminated without further processing.

         Java gives you the advenced technique of handling such errors without abnormal program termination. The advanced mechanism of handling such errors is known as exception handling. Basically there are two types of errors in java namely

  1. Compile time error.
  2. Run time error.

         Compile time errors are the errors generated during the complication of program and easier to rectify because they are generated mostly due to the wrong syntax’s.

Sources of errors:

  1. User input errors: There are typing errors, syntax errors, incorrect or non-existing URL’s.
  2. Device error: These errors occur when there are some problems with devisor such as printers, disc drivers etc.,
  3. Physical Limitation: These errors occur with respect to physical entities or capacities such as space are not available on discs or main memory.
  4. Coding errors: Errors that occur when some logic is wrong, such as locating non-existing files, data structures etc.,

    When an error in a program occurs, we must do the following.
    • All classes are derived from the root class throwable.
    • Error class: This class describes all internal error such as out of disc space, device errors etc., user can be intimated about such errors but you cannot rectify them.

         Java provides you an efficient way of handling runtime error. The mechanism of handling run time error is referred as “exception handling”. In Java exceptions are handled by two ways…

  1. try…catchblock.
  2. throws clause.(Streams)

try…catchblock:

Syntax:

try
{
…....................
Block of code;
…...................
}
catch(Exception object)
{
….................
}

       In case you have a benefit of doubt that there may arise an error in one of the lines of your code, you can simply place the block of code in the “try” block. The “try” block must be accompanied by the “catch” block which takes an object of type “Exception” as an arguments of the try block, the program flow is transferred to the nearest “catch” block and the error generated is recorded in the object. The statements declared in the “catch” block are then executed and the program execution continues after the “catch” block without resulting to “Abnormal program termination”.

import java.lang.*;
class App57
{
public static void main(String yck[])
{
try
{
int x,y,k;
x=Integer.parseInt(yck[0]);
y=Integer.parseInt(yck[1]);
k=x/y;
System.out.println("x="+x);
System.out.println("y="+y);
System.out.println("k="+k);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Executed this line ...");
}
}


output:

\>javac App57.java
\>java App57 10 20
x=10
y=20
k=0
Executed this line ...

\>javac App57.java
\>java App57 10 0
java.lang.ArithmeticException: / by zero
Executed this line ...

Multiple catch blocks:
 A try block can be accompanied by any number of catch blocks.

Syntax:

try
{
…..................
block of code.
…................
}
catch(Exception object1)
{
….................
}
catch(Exception object2)
{
…...................
}
…..................

 When an error occurs in the try blocks, the program flow is transferred to the first catch block. The error generated in the “try” block and the exception specified in the “catch” block and tallied. If both of them are not tallied, the next catch blocks are examined and like wise. Then the statements declared after the last catch block are executed.

import java.lang.*;
class App58
{
public static void main(String yck[])
{
try
{
int x,y,k;
x=Integer.parseInt(yck[0]);
y=Integer.parseInt(yck[1]);
k=x/y;
System.out.println("x="+x);
System.out.println("y="+y);
System.out.println("k="+k);
int p[]=new int[3];
int i;
for(i=0,k=10;i p[i]=k;
System.out.println("Elements of the array ...");
for(i=0;i System.out.println(p[i]);
p[42]=100;
System.out.println(p[42]);
}
catch(ArithmeticException ae)
{
System.out.println(ae);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
System.out.println("Executed this line ...");
}
}


output:

\>javac App58.java
\>java App58 10 20
x=10
y=20
k=0
Elements of the array ...
10
20
30
java.lang.ArrayIndexOutOfBoundsException
Executed this line ...

\>javac App57.java
\>java App57 10 0
java.lang.ArithmeticException: / by zero
Executed this line ...

Nested try:
 You can also have nested try inside your program. A nested try is nothing but try blocks inside another try block. You should keep in view that every try block must be accompanied by its corresponding catch block.

Syntax:
try
{
….................
try
{
….................
}
catch(Exception object1)
{
…..................
}
}
catch(Exception object2)
{
..............….
}
import java.lang.*;
class App59
{
public static void main(String yck[])
{
try
{
int x,y,k;
x=Integer.parseInt(yck[0]);
y=Integer.parseInt(yck[1]);
k=x/y;
System.out.println("x="+x);
System.out.println("y="+y);
System.out.println("k="+k);
try
{
int p[]=new int[3];
int i;
for(i=0,k=10;i p[i]=k;
System.out.println("Elements of the array ...");
for(i=0;i System.out.println(p[i]);
p[42]=100;
System.out.println(p[42]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
System.out.println("Executed this line ...");
}
catch(ArithmeticException ae)
{
System.out.println(ae);
}
}
}


output:

\>javac App59.java
\>java App59 10 20
x=10
y=20
k=0
Elements of the array ...
10
20
30
java.lang.ArrayIndexOutOfBoundsException
Executed this line ...

\>javac App59.java
\>java App59 10 0
java.lang.ArithmeticException: / by zero
Executed this line ...

   Throws clause: Sometimes a method is capable of causing an exception that it does not handle, it must specify this behaviour so that callers of the method can guard themselves against that exception. You do this by including a “throws” clause in the methods’s declaration.

Syntax:

type method_name(parameter list) throws exception_list
{
body of the method;
}

Leave a Reply

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