File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Compilation error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compilation error" Watch "Compilation error" New topic
Author

Compilation error

shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
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

Joined: Jun 19, 2008
Posts: 147
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

Joined: May 30, 2011
Posts: 23

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.


*** If opportunity doesn't knock, build a door !! ***
shivang sarawagi
Ranch Hand

Joined: Jun 19, 2008
Posts: 147
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

Joined: May 30, 2011
Posts: 23

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

Joined: Jun 19, 2008
Posts: 147
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

Joined: Oct 27, 2005
Posts: 19692
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compilation error