Friday, February 27, 2009

example code using throw



package exceptions;
class MinBalance extends Exception
{
MinBalance()
{

}
MinBalance(String str)
{
super(str);
}
}
public class MyException {

private static int accno[]={1001,1002,1003,1004,1005};
private static String name[]={"raju","rani","ramu","sita","vinay"};
private static double bal[]={5000,12000,4540,1999,1200};
public static void main(String arg[])
{
try
{
System.out.println("accno"+"\t"+"name"+"\t"+"balance");
for(int i=0;i<5;i++)
{
System.out.println(accno[i]+"\t"+name[i]+"\t"+bal[i]);
if(bal[i]<2000)
{
MinBalance me=new MinBalance("Balance amount is less");
throw me;
}

}
}catch(MinBalance me)
{
me.printStackTrace();
}
}
}

output:
accno name balance
1001 raju 5000.0
1002 rani 12000.0
1003 ramu 4540.0
1004 sita 1999.0
exceptions.MinBalance: Balance amount is less
at exceptions.MyException.main(MyException.java:28)



0 comments:

Post a Comment