This is probably a rather simple problem, I haven't looked at any Java in some time and am having trouble with this. First off, here is my program:

it's very simple, because I cant even get past here without getting several errors. On Line 6, it says "unreported exception FileNotFoundException; must be caught or declared to be thrown." I thought I was doing this properly with the above try/catch blocks. Then, on line 7, it says "illegal start of type" and on line 11 it says "illegal start of type, ';' expected." Any help anyone can offer would be greatly appreciated, thanks!

EDIT: Nevermind, the problems existed outside of my program! Ignore!

Edited 3 Years Ago by doomsday1216: Solved

You can only put try-catch blocks inside a method body. It is meaningless when placed directly inside a class definition and so the compiler is getting confused and giving you the wrong sort of error message. You probably want most of that stuff to be inside a public static void main(String[] args) method.

Your try block needs to surround the potential source of the exception. Your println could never cause a FileNotFoundException, so it is pointless to wrap it in a try. You want your new FileReader(testFile) to be inside the try block because that is where the exception might come from.

Comments
Thanks!

Yeh, but be careful for "micro-managing exceptions", sometimes you need to put a statement that doesn't throw an exception in a try block to guarantee that it won't execute if an exception is thrown from another statement that comes earlier in that try block.

However, apparently the OP deleted the code from his post (why?)...

Edited 3 Years Ago by mvmalderen

Tux: I think the reason for the deletion can be found in his edit-message:

EDIT: Nevermind, the problems existed outside of my program! Ignore!

Guess he didn't feel like he has to share his code if there's no problem with it, or maybe he removed it so nobody would loose any time by going over it while the problem's not in there.

It would be handy, however, if the thread was marked as "solved", so it would be clear to all that he's no longer looking for an answer.

This article has been dead for over six months. Start a new discussion instead.