• Post Reply Bookmark Topic Watch Topic
  • New Topic

IOException is never thrown in body of corresponding try statement  RSS feed

 
Andrei Chetronie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know why. I put 'throws IOException' after the method name. I have an assignment to write a driving game in command line (all in ascii) and this method gets the X coordinate of the car in the track array. Characters f, g and h move the car left and right or keep it in place.

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does Keyboard.readChar() throw an IOException?

Why do you set a variable to itself? (And, in general, instance variables begin with a lower-case letter: going against convention makes it difficult for others to read your code.)
 
Andrei Chetronie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:Does Keyboard.readChar() throw an IOException?
I just looked it up (the Keyboard class is a class that my lecturer provided and recommend we use for reading input) and the readChar() method doesnt throw IOException. Am I buggered? Are there any other classes I can use?

Why do you set a variable to itself? (And, in general, instance variables begin with a lower-case letter: going against convention makes it difficult for others to read your code.)
Where did I do that?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrei Chetronie wrote:Am I buggered? Are there any other classes I can use?
So, your goal is to throw an IOException? What are you really trying to accomplish? I think things are as simply as: if an exception has no chance of being thrown, don't add it to the throws clause!

Why do you set a variable to itself?
Where did I do that?
Look at your code. It's plain as day.

And to emphasize what David said: not following conventions makes your code surprisingly hard to read.
 
Andrei Chetronie
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to get it to give an error if you enter a character other than f, g or h, or simply more than one character.

edit: Ah, I do see a problem with that now. Never mind what I just said, I just want it to only allow you to enter a single character (before pressing return).
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd probably just validate the input and loop until a legal character is entered.
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andrei Chetronie wrote:. . . Am I buggered? . . .
Please . . . This is a family website. Keep the language down.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!