This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Exception never thrown.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception never thrown...." Watch "Exception never thrown...." New topic
Author

Exception never thrown....

Tom Williams
Greenhorn

Joined: May 10, 2003
Posts: 4
Hi all, wondering if someone could help me out here. I am getting the following error -
exception java.io.FileNotFoundException is never
thrown in body of corresponding try statement
catch (FileNotFoundException exception)
here is the code

If you need any more info let me know.
Thanks - Tom
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Tom,
Welcome to JavaRanch. Apparenly, the GameBoard(String) constructor isn't declared to throw FileNotFoundException.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Tom Williams
Greenhorn

Joined: May 10, 2003
Posts: 4
Hi, thanks for the quick reply. Here is the GameBoard constructor -

All it does is call the parent class constructor with a suitable paramater. What else should it have?
As you can probably tell I am really new to java
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
All it does is call the parent class constructor with a suitable paramater. What else should it have?
Well, what does the super constructor do? If you use a try block that catches a checked exception then some method or constructor must throw that checked excpetion or one of its children somewhere in the block. In your case, that doesn't seem to be what's going on. You could do this:

But that doesn't make a lot of sense unless the call to super(10) can throw that exception. The String (player) that you are passing into the GameBoard constructor is not even being used. I'm guessing that you are wanting to open some file with the name contained in player, in which case, a FileNotFoundException could be thrown.
Tom Williams
Greenhorn

Joined: May 10, 2003
Posts: 4
Ok, i'll try to explain whats going on but it might not make too much sense given that I don't really understand it myself
GameBoard has two constructors, one for a filename typed at the command line and one for a player number if no filename is typed at the command line.
GameBoard is a subclass of Board, Board's function is to display a 10 by 10 grid type board. It has one constructor with an int passed in - super(10)
The main class checks for a filename typed at the command line with args[] (i think) If it doesn't find a filename then it goes ahead and creates a new GameBoard. If there was a filename typed at the command line it does the try/catch. If the filename was correct then it creates a new gameboard with the filename as a parameter. If the filename was incorrect(not found) it displays an error message and exits the program.
Hope that sheds some light on whats going on, let me know if you need anything else and thankyou for replying.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
OK, then you need to do something like this:

That still doesn't seem to fit into your main method or what you described in your last post: that GameBoard has two construtors, one that takes a file name and one for a player number. Instead of having your main method catch FileNotFoundException you probably want to check the file's existence there (just like in the above code) and if the file exists call the constructor that expects a file name otherwise call the other constructor.
Tom Williams
Greenhorn

Joined: May 10, 2003
Posts: 4
I think i've got it, well it compiles anyways.
I didn't have
import java.io.*;
in the gameboard class so I put that in then added
throws IOException
to the constructor that has the filename passed to it.
So, it compiles which is a great leap forward for me, whether it does what it's supposed to do is another story
Thankyou very much for all your help, it is greatly appreciated.
Tom
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception never thrown....
 
Similar Threads
any advice?
Catching non-thrown exceptions
Tic Tac Toe is this good solution
Exceptions-Error in program
Compiler :