Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compilation error

 
shivang sarawagi
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i am compiling this code



the following error is occurring


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type IOException

at Gameguess.gameFunc(Gameguess.java:10)
at GameLauncher.main(GameLauncher.java:6)

i am not able to figure out whats the issue with the code. i have given a catch statement to handle IOException then also the IOException error is occurring.
The compiler is also showing a cross on dr.readLine() method. Please help

i also have two other related classes



&

 
shivang sarawagi
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program is finally running fine, but can anyone tell me the logic of throwing an Exception. I know that the complete calling method stack should throw the exception. Is throwing an exception enough without writing the code for catching it ??
 
Mandy Singh
Greenhorn
Posts: 23
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Buddy,

Have you tried running your code through Notepad ?

I executed it in Eclipse and it worked fine. No Compile time errors. BUT... i get a warning - "The method readLine() from the type DataInputStream is deprecated.

I my case its a warning, but since in your case it gives you an error (as you said there is a cross at the line where you use the dr.readLine() method), I think there might be some setting in Eclipse which would ask the compiler to generate errors for deprecated methods.

You may check that. Please also make sure that the code that you have posted here is the code you are running, because it works fine on my system.
 
shivang sarawagi
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i am running it in eclipse, my coding is running fine too now. Thanks for the concern, but can you tell me why is there a warning for dr.readLine() method ??
 
Mandy Singh
Greenhorn
Posts: 23
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Exceptions is java are handled using the five key words:

1. try - A try block is used to enclose a set of statements which might throw an exception
2. catch - A catch block is used to trap the exceptions that were thrown by some try block. You would be required to write code corresponding to the
exceptional condition.
3. throws - If a method does not handle an exception, then it may list the exception in the method signature using a "throws" clause. This would tell the
caller than the method being called throws an exception and the caller must handle it or may itself choose to throw the exception.
4. throw - This is used to explicitly throw an exception from your code. It might be used to throw some custom exception.
5. finally - A finally block would allow code to be executed whether or not there is an exception raised from a try block.
Even if a return statement is encountered, the finally block shall complete first.

Now onto your question: Is throwing an exception enough without writing the code for catching it ??

See, you must understand that if the main() method of your application also chooses to throw the exception instead of catching it, the exception would be caught by the default mechanism provided by the Java run time system which would print a stack trace for that exception and would cause your application to crash.

You would obviously not want that to happen. :-) So its a good practice to provide a handler and write some sort of code that you would want to be executed when an exception occurs.

By the way, in your code of the "Player" class you have thrown the Exception and also provided a try/catch to handle it. That's not required. Either handle the exception or throw it using the "throws" clause. "throws" tells the compiler that the method does not handle the exception.

Hope that clears some doubts :-)
 
shivang sarawagi
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mandy, will you kindly tell me the about the output you are getting from this program because the random number generator is generating
the same number each and every time.

Working fine sorry my folly, put the random number method outside the loop.
 
Rob Spoor
Sheriff
Pie
Posts: 20669
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mandy Singh wrote:BUT... i get a warning - "The method readLine() from the type DataInputStream is deprecated.

Correct. To read lines, use either a BufferedReader wrapped around an InputStreamReader wrapped around the InputStream, or use a java.util.Scanner.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic