• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Now, if the String length is 0, then reverse() method will throw an exception. But i didn't get any exception, the program compile and run fine. Why??
 
Ranch Hand
Posts: 47
MyEclipse IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which exception you were expecting?
 
Vrushank Shukla
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it would be StringIndexOutOfBound.
 
Ankit Dan
Ranch Hand
Posts: 47
MyEclipse IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From docs

public class StringIndexOutOfBoundsException extends IndexOutOfBoundsException

Thrown by String methods to indicate that an index is either negative or greater than the size of the string. For some methods such as the charAt method, this exception also is thrown when the index is equal to the size of the string.



But in your case there is no such condition and it is fine if you have a zero length string. Using s.charAt(2) will definitely throw a StringIndexOutOfBoundsException

Hope this helps

Ankit
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vrushank Shukla wrote:

Now, if the String length is 0, then reverse() method will throw an exception. But i didn't get any exception, the program compile and run fine. Why??







Your code will not cause an exception because it never enters into the loop at all.
the length of the string that you sent is 0
so the value of i is set to -1 ( length of string - 1) which is 0 - 1
then it checks for condition i >=0 , where it fails and never enters inside the loop
Now it goes to the return statement directly

Hope this helps in getting why the exception dint occur.

 
Vrushank Shukla
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Shashank I got it. Thank you very much.
 
Vrushank Shukla
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And yes Jeff i will mind it. Thank you.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic