Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

question about exception capturing  RSS feed

 
tan kian
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all, i just want to know something. is there an Exception for catching enter key on a readline.charAt(0)(ie. user just presses enter without entering anything else thus passing an empty string)?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you write something like

and readLine() returns "" (the empty String) then yes, charAt() will throw a StringIndexOutOfBoundsException, as there is no 0th character in an empty String. Note also that at end-of-file, readLine() returns null, and you need to watch for that as well. Better to write something like
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!