Thursday, 10 July 2014

Exception Handling

Exception Handling

 It deals with the way errors can be handled. Error can be complie time ,run time ,logical. In exception handling we deal with errors caused at run time. For eg. in division of 2 no's if denominator is assigned a value 0 this will affect the output, an error will be generated. Exception handling deals with such type of errors. Exception classes can be in built or they can be user defined.

Some in built classes are-
1.Arithmetic Exception- to handle arithmetic errors like 0 in denominator.
2.Array Index Out Of Bounds- used when the position assigned to some data in array is not present.
3.Array Store Exception- assignment to an array of an incompatible type.
4.Class Cast Exception- for invalid casting.
5.Illegal Argument Exception- use of illegal arguments.
6.Illegal State Exception- environment or application is in incorrect state.
7.Index Out Of Bounds- Some type of indexes are out of bounds.
8.Negative Array Size Exception- Array created with a negative size.
9.Null pointer exception- use of null reference.
10.Number format exception-invalid conversion of string to numeric format.
Programs regarding some in built exceptions:-
1.Array Out Of Bounds Exception:
import java.io.*;
class Arroutbounds
{
public static void main(String arg[])
{
int a[]= new int[10];
int b,i;
DataInputStream dis = new DataInputStream(System.in);
try
{
try
{
System.out.println("enter no of integer's uh want to enter");// output:enter no of integer's uh want to              
b=Integer.parseInt(dis.readLine());            //15                                                                         //enter
for(i=0;i<b;i++)
{
a[i]=Integer.parseInt(dis.readLine());//output:1234567891112
}
for(int j=0;j<b;j++)
{
System.out.println(a[j]);
}
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Beyond the limits");    //output:Beyond the limits
}
}
catch(IOException io)
{}
}

}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
2.Number Format Exception:
import java.io.*;
class Noformats
{
public static void main(String arg[])
{
int a;
DataInputStream dis= new DataInputStream(System.in);
try
{
try
{
System.out.println("enter int value");    //output:enter a value
a=Integer.parseInt(dis.readLine());        //output:struy
}
catch(NumberFormatException e)
{
System.out.println("data entered is incompatible");    //output:data entered is incompatible
}
}
catch(IOException io){}
}

}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
3.Arithematic Exception:
import java.io.*;

class Excep
{
public static void main(String arg[])
{

DataInputStream dis = new DataInputStream(System.in);


try
{
int a,b,c;
a=Integer.parseInt(dis.readLine());       //output: 4
b=Integer.parseInt(dis.readLine());      //output:0
try
{
c=a/b;
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println("denominator cannot be 0");   //output:denominator cannot be 0
}
finally
{
System.out.println("this is the final block");        //output:this is the final block
}
}
catch(IOException io){}

}

}

No comments:

Post a Comment