Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unexpected Exception

 
Kaushal Wadhwani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following program

class kaushal
{
public static void main(String args[])
{


try
{
for(int i=0;i<=args.length;i++)
{
System.out.println("\n array is : " +args[i]);
}
}
catch(Exception e)
{
System.out.println("Error is coming from out of bound ");
}
}
}

after compilin nd executing : java kaushal 2 3 5

the output is :

array is : 2

array is : 5

array is : 3
Error is coming from out of bound


why is this exception being generated each time...can someone please explain ?
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



arrays start from 0. so think what should be check condition.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[CODE] tags make posts beautiful. Let's use them.

the check in the for loop is checking beyond limits.
 
Kaushal Wadhwani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Balu Sadhasivam

i got my answer
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Good.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic