Monday, 20 April 2015

EXCEPTION-HANDLING-IN-JAVA

(1)SIMPLE   TRY CATCH:
package first;

public class excep1 {
public static void main(String []s)
{
try
{
int a=10,b=0,c;
c=a/b;
System.out.println("the ans is "+c);
}catch(Exception e){System.out.println ("divident can not be zero");}
}

}

(2)NESTED TRY CATCH:
package first;

public class ntry {
public static void main(String []s)
{
int x[]={45,67};
try
{
int a=10,b=0,c;
c=a/b;
try
{
x[0]=45;
x[1]=22;
}catch(ArrayIndexOutOfBoundsException e){System.out.println(e);}
}catch(ArithmeticException e1){System.out.println("divisior can not be zero");}
finally
{
x[1]=23;
System.out.println(x[0]+" :"+x[1]);
}
}
}

(3)MULTIPLE  CATCH:

package first;

public class mcatch {
public static void main(String []s)
{
try
{
int a=10,b=1,c;
c=a/b;
int x[]={45,67};
x[20]=45;
}catch(ArrayIndexOutOfBoundsException e){System.out.println("index should be in range");
}catch(ArithmeticException e1){System.out.println("divisior can not be zero");}
}
}

(4)Custom  Exception:

package first;

public class custexcep {
public static void main(String []om)
{
int m=134;
try
{
 if(m>100)
 throw new myexception ("marks exceeed the limit");   //human h=new human();
 System.out.println("marksl are"+m);
}catch(myexception e)
{System.out.println(e.getMessage());}
}

}
class myexception extends Exception
{
myexception (String s)
{
super(s);
}
}

No comments:

Post a Comment