aspose file tools*
The moose likes Beginning Java and the fly likes Unexpected Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Unexpected Exception" Watch "Unexpected Exception" New topic
Author

Unexpected Exception

Kaushal Wadhwani
Greenhorn

Joined: Aug 22, 2009
Posts: 5
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

Joined: Jan 01, 2009
Posts: 874




arrays start from 0. so think what should be check condition.
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
[CODE] tags make posts beautiful. Let's use them.

the check in the for loop is checking beyond limits.


Experience and talent are independent of age
Kaushal Wadhwani
Greenhorn

Joined: Aug 22, 2009
Posts: 5
thank you Balu Sadhasivam

i got my answer
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874


Good.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unexpected Exception
 
Similar Threads
exam objective 4.3 cmd-line args to main
casting
Two Dimensional Arrays
ArrayOutofbound
Why i can't print this array?