File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Review the code Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Review the code" Watch "Review the code" New topic
Author

Review the code

sharon daze
Greenhorn

Joined: Feb 08, 2009
Posts: 16
Anyone please explain me why this code always goes into catch even exception is not thrown.



[HENRY: Add Code Tags]


http://myjvm.wordpress.com
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16681
    
  19

Anyone please explain me why this code always goes into catch even exception is not thrown


Well, if you had printed the exception in the catch block, you can see the reason for yourself.

But just by "eyeballing" (without running it), my guess would be because of an out of bounds exception.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
sharon daze
Greenhorn

Joined: Feb 08, 2009
Posts: 16
Yes you are right
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12907
    
    3

Please use code tags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Henry Zhi Lin
Ranch Hand

Joined: Nov 04, 2008
Posts: 69
sharon daze wrote:Anyone please explain me why this code always goes into catch even exception is not thrown.



That's the bit of code causes your exception. String index start from 0 to String.length() -1, and you pass String.length will result index out of bound exception.

SCJP 5.0
 
I agree. Here's the link: jrebel
 
subject: Review the code
 
Similar Threads
error in programe
how to convert from string("a") to char..
how to do this? (exception) can u help me to fix?
[Easy] Shift String
about exception