Thursday 11 August 2016

Exception Handling in java ( ArrayIndexOutOfBoundsException)

class MyException extends Exception
{
String s1;
MyException(String s1)
{
this.s1=s1;
}
public String toString()
{
return(s1);
}
}
class Myclass
{
public static void main(String args[])
{
int marks[]={59,33,99,100,97};
int stno[]={1,2,3,4,5};
try
{
for(int i=0;i<6;i++)
{
if (marks[i]>100)
throw new MyException("invaid data");
if (i<4)
System.out.println("rollno="+stno[i]+"\tmarks="+marks[i]);
else
throw new ArrayIndexOutOfBoundsException("Array out of bnds");
}
}
catch(MyException e)
{
System.out.println("Exception="+e);
}
catch(ArrayIndexOutOfBoundsException a)
{
System.out.println("Index out of range"+a.getMessage());
}
finally
{
System.out.println("finished");
}
}
}

No comments: